Multiple issues with program that draws on a panel

Joined
Mar 19, 2008
Messages
5
Location
Western NY
Programming Experience
1-3
I have an application that allows a user to choose a color and the width of the pen to draw with on the included panel. I have most it working properly except for a few remaining pieces. First whenever the user starts drawing the lines and panel visibly flicker and I cannot figure out how to resolve it. I tried things like doublebuffer but it did not work. It is probably something I did wrong as this is my first attempt at drawing in vb.net.

My second issue is that if you draw a line with one color and than switch to another color the previously drawn lines, etc. change to the new color. I want them to stay the same but cannot figure out how.

My last question is I can get the radiobuttons to work in terms showing only the correct one as checked. However the width of the pen does not change when used to draw. I was able to get it to change with the code below but it gets thinner instead of wider as the width listed gets higher plus like the color issue the width of previously drawn lines changes as well which it shouldn't

Below is the code I currently have

VB.NET:
Public Class frmDrawingPad

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

        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 Then ' draw a filled cline if left mouse is down  

            Try
                mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath

            Catch
                MsgBox("No way, Hose!")
            End Try

        End If

        panelDraw.Invalidate()

    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

            e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path!

        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 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
        Select Case i
            Case 0
                penWidth = 1
            Case 1
                penWidth = 5
            Case 2
                penWidth = 10
            Case 3
                penWidth = 20
        End Select

    End Sub

End Class
 

Latest posts

Back
Top