Problem changing color and width in drawing program
-
I'm trying to write a basic program for a class project where the user can draw on a panel. The user can choose which color to use and the width of the pen. I have everything working except if the user decides to draw something in one width and color and than switch to another width and/or color the previously drawn items also change to the new values. I want the previous items to remain as they were and only newly drawn items to take on the new conditions. I have tried a number of things to no avail and could really use some help. I have included my code below for review:
Public Class frmDrawingPad
Dim draw As Boolean = False
Dim index As Integer = 0 ' sets index for combobox control
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath()
Dim userColor As New Color() 'this is a color the user selects
Dim penWidth As Single
Dim rbChecked(3) As RadioButton
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' exits program
Me.Close()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
' exits program
Me.Close()
End Sub
Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
' calls procedure that clears panel
Call ClearDrawingPanel()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' calls procedure that clears panel
Call ClearDrawingPanel()
End Sub
Private Sub panelDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelDraw.MouseDown
If e.Button = MouseButtons.Left Then ' draw a filled line if left mouse is down
mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
draw = True
End If
End Sub
Private Sub panelDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelDraw.MouseMove
If e.Button = MouseButtons.Left And (draw) Then ' draw a filled line if left mouse is down and draw is true
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
panelDraw.Invalidate() ' adds new line to next update of panelDraw which will take place at the next paint event
Catch
MsgBox("No way, Hose!")
End Try
End If
End Sub
Private Sub panelDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles panelDraw.MouseUp
' changes draw boolean back to false
draw = False
End Sub
Private Sub panelDraw_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles panelDraw.Paint
Try ' error trapping
'*********************** NOTE ***********************************************
'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
' A simpler, but less flexible solution would be to replace the line with the following code:
'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
'************ End Note ***************************
Dim CurrentPen = New Pen(userColor, penWidth) 'Set up the pen
Dim linecap As Drawing2D.LineCap = Drawing2D.LineCap.Round
CurrentPen.StartCap = linecap
CurrentPen.EndCap = linecap
e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path
CurrentPen.Dispose()
Catch
' MsgBox("Not happening!")
End Try
End Sub
Public Sub ClearDrawingPanel()
' clears drawing panel
Dim g As Graphics = panelDraw.CreateGraphics
g.Clear(panelDraw.BackColor)
mousePath.Reset()
g.Dispose()
End Sub
Private Sub frmDrawingPad_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' loads list of colors to combobox
cboColorPick.Items.Add("Pick A Color")
cboColorPick.Items.Add("Black")
cboColorPick.Items.Add("Blue")
cboColorPick.Items.Add("Pink")
cboColorPick.Items.Add("Red")
cboColorPick.Items.Add("Green")
cboColorPick.Items.Add("Yellow")
cboColorPick.Items.Add("Purple")
' assigns radiobuttons to array
rbChecked(0) = rbWidth1
rbChecked(1) = rbWidth5
rbChecked(2) = rbWidth10
rbChecked(3) = rbWidth20
End Sub
Private Sub cboColorPick_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboColorPick.SelectedIndexChanged
' assigns to index the combobox index of the selected color
index = cboColorPick.SelectedIndex
' calls procedure that assigns color to pen
Call AssignPenColor(index)
End Sub
Private Sub BlackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlackToolStripMenuItem.Click
index = 1
Call AssignPenColor(index)
End Sub
Private Sub BlueToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 2
Call AssignPenColor(index)
End Sub
Private Sub PinkToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 3
Call AssignPenColor(index)
End Sub
Private Sub RedToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 4
Call AssignPenColor(index)
End Sub
Private Sub GreenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 5
Call AssignPenColor(index)
End Sub
Private Sub YellowToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 6
Call AssignPenColor(index)
End Sub
Private Sub PurpleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
index = 7
Call AssignPenColor(index)
End Sub
Private Sub AssignPenColor(ByVal index As Object)
' takes passed index collected from selection in combobox and checks to see which case it matches. once correct one is found
' the userColor variable is assigned the appropriate color
Select Case index
Case 1
userColor = Color.Black
Case 2
userColor = Color.Blue
Case 3
userColor = Color.Pink
Case 4
userColor = Color.Red
Case 5
userColor = Color.Green
Case 6
userColor = Color.Yellow
Case 7
userColor = Color.Purple
End Select
End Sub
' handles checkedchanged event for all radiobuttons
Private Sub rbWidth1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbWidth1.CheckedChanged, _
rbWidth5.CheckedChanged, rbWidth10.CheckedChanged, rbWidth20.CheckedChanged
penWidth = 0
Dim i As Integer = 0
Dim found As Boolean = False
' runs through loop until it finds the radiobutton which was selected within the rbChecked array
While i < rbChecked.GetLength(0) And Not found
If rbChecked(i).Checked Then
found = True
End If
i += 1
End While
' once index is found it is evaluated to determine which width value should be assigned to penWidth variable
' an additional 1 is added to i as it leaves loop so to get accurate value we need to subtract out that added one
Select Case i - 1
Case 0
penWidth = 1.0F
Case 1
penWidth = 5.0F
Case 2
penWidth = 10.0F
Case 3
penWidth = 20.0
End Select
End Sub
End Class
Post a reply
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).