Drawing On a Panel

DonnaF

Active member
Joined
Mar 20, 2003
Messages
30
I have a program that is allowing a user to draw on a form, using MouseMove, MouseDown, and MouseUp events. I have added radio buttons, to allow the user to change the color and size of what they are drawing, plus a panel for the drawing area. How do I change my code from drawing on a form, to drawing on a panel? Heres my current code. Thanks, Donna

Public Class frmPainter
Inherits System.Windows.Forms.Form


Dim shouldPaint As Boolean = False
Dim newColor As Color = Color.Red

Private Sub frmPainter_MouseMove( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove

If shouldPaint Then
Dim graphic As Graphics = CreateGraphics()

graphic.FillEllipse _
(New SolidBrush(newColor), e.X, e.Y, 4, 4)
End If
End Sub

Private Sub frmPainter_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown

shouldPaint = True

End Sub

Private Sub frmPainter_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp

shouldPaint = False
End Sub

End Class
 
You dont have to change much. Just change the event to handle the panel instead of the form:
Code:
Handles Panel1.MouseMove
And then when you create a graphics object use:
Code:
Panel1.CreateGraphics()
Instead of forms CreateGraphics method.
 
Back
Top