VBNet Create a Windows Form application that allows the user
VB.Net
Create a Windows Form application that allows the user to draw free-hand images with the mouse in a PictureBox control. The application should have three menu items: pen color, pen width, and clear. The pen color menu item should display a ColorDialog to allow the user to select the pen color. The pen width menu item should allow the user to choose between the sizes 5, 10, 15, and 20 graphic units. The clear menu item will clear the PictureBox control by calling its Invalidate method.
Solution
Public Class Picture
\'Set shouldpaint to true/false to use for mousedown/mouseup
Dim shouldpaint As Boolean = False
\'Form load section
Private Sub Picture_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ColorBox.SelectedItem(0) = Color.Aqua
ColorBox.SelectedItem(1) = Color.Blue
ColorBox.SelectedItem(2) = Color.Red
End Sub
Private Sub Drawpb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseDown
\'Set drawpd to draw when mouse was held down
shouldpaint = True
End Sub
Private Sub Drawpb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseUp
\'Set drawpd not to draw when mouse was up
shouldpaint = False
End Sub
Private Sub Drawpb_mousemove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseMove
If shouldpaint Then
\'Declare g for graphics
Dim g As System.Drawing.Graphics
\'set drawpad as site for freehand drawing
g = Drawpb.CreateGraphics()
\'Set brush color as black and size of brush to 20, 20
g.FillEllipse( _
New SolidBrush(Color.Black), e.X, e.Y, 20, 20)
End If
End Sub
\'Exit Button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
\'End program
Me.Close()
End
End Sub
\'Clear Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
\'Clear Drawpad
Drawpb.Refresh()
End Sub
\'Menu strip: Close
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
\'End Program
Me.Close()
End
End Sub
\'Menu Strip: Clear
Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
\'Clear Drawpad
Drawpb.Refresh()
End Sub
\'Color ComboBox
Private Sub ColorBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorBox.SelectedIndexChanged
End Sub
End Class

