chriswallis
Programmer
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
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