Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In one form of my VB.NET Windows application, I have a picturebox. I need to draw a line (using known coordinates) on picturebox when the form is loading. Following is the code I wrote in my Form_Load event.

 

Private Sub frm_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Handles MyBase.Load

 

PictureBox1.Refresh()

 

Dim g As Graphics = CType( PictureBox1, PictureBox).CreateGraphics

 

Dim myPen As New Pen(Color.Red)

myPen.Width = 2

 

g.DrawLine(myPen, 0, 0, 100, 100)

 

myPen.Dispose()

g.Dispose()

 

End Sub

 

 

But it's not displaying the Line when the Form is loading. I cannot figure out why.

 

Thank you

Posted

I tried you code and as you say it does not work, I suspect this is because you are using the load event to draw and the timing is such that the line is rubbed out before the form is properly loaded.

You should do all you drawing in the paint event so:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

       Dim myPen As New Pen(Color.Red)
       myPen.Width = 2

       e.Graphics.DrawLine(myPen, 0, 0, 100, 100)
       myPen.Dispose()


   End Sub

 

Notice I am using the graphics object supplied by the paint event, do not use CreateGraphics!

 

Hope this helps

  • Leaders
Posted

Quite right, the .Load event is called immediately BEFORE the form is shown for the first time. Any drawing you do before the form is shown will never show up.

Also, FYI, the pen constructor has an overload like this (color As System.Drawing.Color, width As Single).

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...