Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paint event problem

Status
Not open for further replies.

chriswallis

Programmer
Joined
Feb 22, 2002
Messages
9
Location
GB
I am struggling to understand when the paint event should
fire, since I have created a very simply app to simply
draw a line on a picturebox whenever the paint event
fires. Unfortunately the line only appears when I move
another window over the picturebox, not when the form is
restored from being minimized or focus is switched to
another window and back again. How can I ensure that the
line is always painted?

The app is very simply. Create a form with a picturebox
called picMain and add the following code.

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
DrawLine()
End Sub

Private Sub DrawLine()

Dim g As Graphics
Dim oPen As New Pen(Color.Black)

Try
g = picMain.CreateGraphics
g.DrawLine(oPen, 0, 0, 200, 200)
Finally
oPen.Dispose()
oPen = Nothing
g.Dispose()
g = Nothing
End Try
End Sub

Private Sub picMain_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) Handles
picMain.Paint
DrawLine()
End Sub

 
(Let me prefix this statment with that I never have used the drawing thing before)

I know this does not solve your problem, but I copied the code over and I tried it many different ways. I works fine if you based it off the form and not the picture box. I was having the same problems as you, but one thing I noticed was if you resize the form by grabbing the edges of the form the line will show up (note: I anchored the picturebox on all four sides). Otherwise the line will show up and disappear (very quickly).

Not that this solves the problem but I found a shorter way to write the code.

Private oPen As New Pen(Color.Black)
...
Private Sub picMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
picMain.CreateGraphics.DrawLine(oPen, 0, 0, 200, 200)
End Sub
 
I found another person with the same problem. It does not list a solution, but it does have his email address. Send him a line and see if he solved the problem.



Entire Thread
Author: Jason (oldfriend99@hotmail.com)
Subject: vb.net drawing in a picturebox?
Newsgroup: microsoft.public.vb.controls
Date: 04/23/2002 at 7:02 AM

.....
 
I have solved this myself. The trick is to pass in the Graphics object to the DrawLine function from the paint event. ie:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DrawLine(picMain.CreateGraphics)
End Sub

Private Sub DrawLine(g as Graphics)

Dim oPen As New Pen(Color.Black)

Try
g.DrawLine(oPen, 0, 0, 200, 200)
Finally
oPen.Dispose()
oPen = Nothing
g.Dispose()
g = Nothing
End Try
End Sub

Private Sub picMain_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) Handles
picMain.Paint
DrawLine(e.graphics)
End Sub

I guess this is all to do with when DeviceContexts are created and destroyed for the picturebox...



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top