PC Review


Reply
Thread Tools Rate Thread

Draw Line of Form?

 
 
Ed Bitzer
Guest
Posts: n/a
 
      3rd Apr 2005
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.FromImage(bit)
Dim myPen As Pen = New Pen(Color.Blue, 1)
Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5)
Appreciate some help.

Ed


 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      3rd Apr 2005
Ed,

"Ed Bitzer" <(E-Mail Removed)> 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.FromImage(bit)
> Dim myPen As Pen = New Pen(Color.Blue, 1)
> Me.CreateGraphics.DrawLine(myPen, 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.DrawLine(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 ('CreateGraphics', '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/>

 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      3rd Apr 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.FromImage(bit)
> Dim myPen As Pen = New Pen(Color.Blue, 1)
> Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5)
> Appreciate some help.
>
> Ed
>



 
Reply With Quote
 
Martin Horn
Guest
Posts: n/a
 
      3rd Apr 2005
>
> Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
> you created yourself ('CreateGraphics', '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.


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      3rd Apr 2005
Martin,

"Martin Horn" <(E-Mail Removed)> schrieb:
>> Don't fotget to call the 'Dispose' method of 'Graphics' and 'Pen' objects
>> you created yourself ('CreateGraphics', '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/>

 
Reply With Quote
 
Crouchie1998
Guest
Posts: n/a
 
      3rd Apr 2005
Here's an MSDN sample of how to create Line Controls:

http://download.microsoft.com/downlo...neControls.msi


 
Reply With Quote
 
Ed Bitzer
Guest
Posts: n/a
 
      4th Apr 2005
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]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:O%(E-Mail Removed)...
> Ed,
>
> "Ed Bitzer" <(E-Mail Removed)> 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.FromImage(bit)
>> Dim myPen As Pen = New Pen(Color.Blue, 1)
>> Me.CreateGraphics.DrawLine(myPen, 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.DrawLine(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 ('CreateGraphics', '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/>



 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      4th Apr 2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.FromImage(bit)
> > Dim myPen As Pen = New Pen(Color.Blue, 1)
> > Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5)
> > Appreciate some help.
> >
> > Ed
> >

>
>
>

 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      4th Apr 2005
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" <(E-Mail Removed)> wrote in message
news:75502B37-55A1-44C0-ACF3-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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.FromImage(bit)
>> > Dim myPen As Pen = New Pen(Color.Blue, 1)
>> > Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5)
>> > Appreciate some help.
>> >
>> > Ed
>> >

>>
>>
>>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      4th Apr 2005
Ed,

"Ed Bitzer" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: draw line on a form/panel and remove/clear the line? rowe_newsgroups Microsoft VB .NET 0 18th Jan 2007 02:51 AM
Re: draw line on a form/panel and remove/clear the line? Ray Cassick Microsoft VB .NET 0 18th Jan 2007 12:10 AM
How to draw a line in a Windows Form? Jason Huang Microsoft C# .NET 4 29th Nov 2005 02:03 PM
Draw a Line on a windows form eRic Microsoft Dot NET Framework Forms 7 9th Oct 2003 08:40 PM
Draw a line on a form Steve Microsoft VB .NET 3 9th Jul 2003 05:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:11 PM.