473,574 Members | 18,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Draw Line of Form?

Can draw a line on my form with a button click event but cannot upon the
Load event when I wish. No more line object so used the following:
Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
Dim g As Graphics = Graphics.FromIm age(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)
Appreciate some help.

Ed
Nov 21 '05 #1
18 8538
Ed,

"Ed Bitzer" <ed******@yahoo .com> schrieb:
Can draw a line on my form with a button click event but cannot upon the
Load event when I wish. No more line object so used the following:
Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
Dim g As Graphics = Graphics.FromIm age(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)

At the time when the form's 'Load' event is raised, the form is not yet
visible. Thus the drawing will not be visible when the form is shown. In
..NET's drawing model, drawings need to be renewed whenever a part of the
form which is obscured gets visible again. You may want to place your
drawing code in the form's 'Paint' event handler or an overridden 'OnPaint'
method. These methods will be called whenever a part of the form needs to
be redrawn:

\\\
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint( e)
e.Graphics.Draw Line(Pens.Blue, 0, 5, Me.Width, 5)
End Sub
///

Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
you created yourself ('CreateGraphic s', 'New Pen(...)') when you don't need
them any more.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
See the GDI+ FAQ for explanations of why CreateGraphics is a bad idea.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Ed Bitzer" <ed******@yahoo .com> wrote in message
news:O0******** ******@TK2MSFTN GP15.phx.gbl...
Can draw a line on my form with a button click event but cannot upon the
Load event when I wish. No more line object so used the following:
Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
Dim g As Graphics = Graphics.FromIm age(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)
Appreciate some help.

Ed

Nov 21 '05 #3
>
Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
you created yourself ('CreateGraphic s', 'New Pen(...)') when you don't
need them any more.


Doesn't the Garbage Collector handle this sort of thing once they go out of
scope? Apologies if this is a silly question.

Martin.
Nov 21 '05 #4
Martin,

"Martin Horn" <mv****@notThis Bit.ntlworld.co m> schrieb:
Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
you created yourself ('CreateGraphic s', 'New Pen(...)') when you don't
need them any more.


Doesn't the Garbage Collector handle this sort of thing once they go out
of scope? Apologies if this is a silly question.


..NET doesn't support deterministic finalization of objects as known from
COM/VB6. The GC will destroy the object, but this will not occur
immediately when all reachable references to the object are released.
'Graphics' and 'Pen' use unmanaged resources (GDI handles). By calling
'Dispose' directly these resources are released immediately, which can
prevent the application from missing unmanaged resources.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
Here's an MSDN sample of how to create Line Controls:

http://download.microsoft.com/downlo...neControls.msi
Nov 21 '05 #6
Herfried,

Understand and as indicated if I place in the Paint event I the lines are
displayed. Of course I tried to extrapolate my new knowledge but was not
able to use the same logic with a PictureBox. If I add a PictureBox to the
load event on my form and then try to draw lines using the same approach
placing the line drawing code in the PictureBox even - no work. I of course
did change all references to Me in the Form example to PictureBox. Is that
as simply explainable?

Ed

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:O%******** ********@tk2msf tngp13.phx.gbl. ..
Ed,

"Ed Bitzer" <ed******@yahoo .com> schrieb:
Can draw a line on my form with a button click event but cannot upon the
Load event when I wish. No more line object so used the following:
Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
Dim g As Graphics = Graphics.FromIm age(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)

At the time when the form's 'Load' event is raised, the form is not yet
visible. Thus the drawing will not be visible when the form is shown. In
.NET's drawing model, drawings need to be renewed whenever a part of the
form which is obscured gets visible again. You may want to place your
drawing code in the form's 'Paint' event handler or an overridden
'OnPaint' method. These methods will be called whenever a part of the
form needs to be redrawn:

\\\
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint( e)
e.Graphics.Draw Line(Pens.Blue, 0, 5, Me.Width, 5)
End Sub
///

Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
you created yourself ('CreateGraphic s', 'New Pen(...)') when you don't
need them any more.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
Bob, I've seen a lot of posts saying CreateGraphics is a bad Idea, mostly
they say because of encapuslation and it doesn't work with double buffering.
I've yet to really understand why it is a bad to use it so long as one
doesn't use double buffering...is there some fear that this won't work in
future releases of VB.Net and if so, why aren't we worried about several
other things the probably will change in future releases? Why the
CreateGraphics worry..do you have some insight as to what Microsoft will do?

"Bob Powell [MVP]" wrote:
See the GDI+ FAQ for explanations of why CreateGraphics is a bad idea.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Ed Bitzer" <ed******@yahoo .com> wrote in message
news:O0******** ******@TK2MSFTN GP15.phx.gbl...
Can draw a line on my form with a button click event but cannot upon the
Load event when I wish. No more line object so used the following:
Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
Dim g As Graphics = Graphics.FromIm age(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)
Appreciate some help.

Ed


Nov 21 '05 #8
The problem with using the CreateGraphics call for any purpose other than
obtaining data about the Graphics object of a particular window is that it
breaks the fundamental principle of event driven programming.

Windows is an event driven system in which things happen in order and for a
purpose. When you write code that refuses to follow the order of events as
dictated by the system you will have your own misguided efforts walked upon
by the *correct functioning* of the system.

Painting using CreateGraphics will almost inevitably be overprinted by the
OnPaint method and causes huge numbers of questions from inexperienced
programmers who think that there is some sort of system error when it's
their own code that's at fault. This is why it's still the GDI+ FAQ most
asked question despite three years of the answer being available for all to
see with a simple google search.

It's also why I generally answer such questions with a brusque "RTFM" style
answer so, after you've "Read That Fine Material" ;-) you'll understand
fully.

Microsoft's operating systems, including Longhorn, will continue to be event
driven and will exhibit the same behaviours if event order is ignored
whether it's GDI, GDI+ or Avalon doing the drawing.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:75******** *************** ***********@mic rosoft.com...
Bob, I've seen a lot of posts saying CreateGraphics is a bad Idea, mostly
they say because of encapuslation and it doesn't work with double
buffering.
I've yet to really understand why it is a bad to use it so long as one
doesn't use double buffering...is there some fear that this won't work in
future releases of VB.Net and if so, why aren't we worried about several
other things the probably will change in future releases? Why the
CreateGraphics worry..do you have some insight as to what Microsoft will
do?

"Bob Powell [MVP]" wrote:
See the GDI+ FAQ for explanations of why CreateGraphics is a bad idea.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Ed Bitzer" <ed******@yahoo .com> wrote in message
news:O0******** ******@TK2MSFTN GP15.phx.gbl...
> Can draw a line on my form with a button click event but cannot upon
> the
> Load event when I wish. No more line object so used the following:
> Dim bit As Bitmap = New Bitmap(Me.Width , Me.Height)
> Dim g As Graphics = Graphics.FromIm age(bit)
> Dim myPen As Pen = New Pen(Color.Blue, 1)
> Me.CreateGraphi cs.DrawLine(myP en, 0, 5, Me.Width, 5)
> Appreciate some help.
>
> Ed
>


Nov 21 '05 #9
Ed,

"Ed Bitzer" <ed******@yahoo .com> schrieb:
Understand and as indicated if I place in the Paint event I the lines are
displayed. Of course I tried to extrapolate my new knowledge but was not
able to use the same logic with a PictureBox. If I add a PictureBox to
the load event on my form and then try to draw lines using the same
approach placing the line drawing code in the PictureBox even - no work.
I of course did change all references to Me in the Form example to
PictureBox. Is that as simply explainable?


I assume that you took the 'OnPaint' code I posted and tried to use it for a
picturebox placed on a form. This won't work, because 'OnPaint' is a
protected method that can only be overridden in a class that derives from
the base class containing this method. So, there are two solutions:

Simple solution: Instead of overriding 'OnPaint', simply add a handler to
the picturebox's 'Paint' event. To do that, open the code editor window,
choose the picturebox from the left combobox (which is on top of the code
editor) and select 'Paint' in the combobox on the right side. An event
handler will be generated automatically. All you need to do is simply
entering your drawing code in the event handler.

Complicated solution: Create a new class that inherits from 'PictureBox'
and overrides its 'OnPaint' method. Then use this extended picturebox class
instead of the standard picturebox control.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9693
by: Dennis | last post by:
Hello, Ive to draw a line on a picture i used this code: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) x1 = z.X y1 = z.Y lbl3 = "x1: " & x1 lbl4 = "y1: " & y1
3
15967
by: Steve | last post by:
Hi All, I am sure this is a simple question, but how do I draw a line onto a windows form in VB.NET, or any shape for that matter. VB6 had a line and shape control that you could use. Thanks, Steve
3
10315
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5...
4
5146
by: thomasp | last post by:
I found the following code on MSDN to draw a line in VB2005. Public Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 3) ' Create points that define line. Dim point1 As New Point(100, 100) Dim point2 As New Point(500, 100)
2
3115
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I get all kinds of screen artifacts and weirdness. My general goal is: list item with a few areas for text, every other item shaded a light color for...
4
30796
by: Jason Huang | last post by:
Hi, In my C# Windows Form, how do I draw a Line? Thanks for help. Jason
1
2111
by: Rich | last post by:
Hello, I have a form with a panel which contains a radiobutton. When I click the radiobutton, I invoke the Paint event of the panel using me.Invalidate. The paint event gets called and runs through code to draw a line. But the line does not draw on the panel until I press either the Alt key or Tab key. Here is my code: Sub...
1
2556
by: Alejandro | last post by:
Hi, I Have a form with Collection of 52 picturebox. Public cl As New Collection Private m_Bitmap As Bitmap Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
9
4016
by: zhaow | last post by:
Hi, All Greetings! I want to develop as appllication that requires a line-drawing function in the blank area between two forms. I have looked up the MSDN, it says that a graphics object need a reference to a control or a form. I guess it means that lines can't be draw on blank area between two forms. Can anybody guarantee this for me? Is...
0
7817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8258
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1082
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.