My paint program

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Below is all of my code. It works, except the PenWidth. I can't figure out what I'm doing wrong.

VB.NET:
Option Strict On
Public Class Form1
    Dim MyPen As New Pen(Color.Black)
    Dim PenColor As Color
    Dim IsThePenDown As Boolean
    Dim MouseDownPoint As Point
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        picBoxOutput.BackColor = Color.White
        cmbSelectColor.Items.Add("Red")
        cmbSelectColor.Items.Add("Orange")
        cmbSelectColor.Items.Add("Yellow")
        cmbSelectColor.Items.Add("Green")
        cmbSelectColor.Items.Add("Blue")
        cmbSelectColor.Items.Add("Indigo")
        cmbSelectColor.Items.Add("Violet")
        cmbSelectColor.Items.Add("Brown")
        cmbSelectColor.Items.Add("White")
        cmbSelectColor.Items.Add("Gray")
        cmbSelectColor.Items.Add("Black")
        cmbSelectColor.Items.Add("Pink")
        cmbSelectColor.Items.Add("Magenta")
        cmbSelectColor.Items.Add("Cyan")
    End Sub
    Private Sub ClearThePictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearThePictureToolStripMenuItem.Click
        'Call the ClearPic SUB.>>  
        Call ClearPic()
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Call the ClearPic SUB.>>  
        Call ClearPic()
    End Sub
    Private Sub ClearPic()
        'Clear the PictureBox to WHITE.>>  
        picBoxOutput.CreateGraphics.Clear(Color.White)
    End Sub
    Private Sub SetpenColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetpenColorToolStripMenuItem.Click
        'Show the ComboBox to select a color.>>  
        cmbSelectColor.Show()
    End Sub
    Private Sub SetPenwidthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetPenwidthToolStripMenuItem.Click
        'Show the Panel to Set the Pen Width.>>  
        pnlSetPenWidth.Show()
    End Sub
    Private Sub cmbSelectColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSelectColor.SelectedIndexChanged
        'Set the pen color to the selected color.>>  
        MyPen.Color = Color.FromName(cmbSelectColor.SelectedItem.ToString)
    End Sub
    Private Sub rdoBtnWidth_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoBtnWidth1.CheckedChanged, rdoBtnWidth2.CheckedChanged, rdoBtnWidth3.CheckedChanged, rdoBtnWidth4.CheckedChanged
        'Used to hold the Pen width.>>  
        Dim PenWidth As Integer
        'Usedto hold which radioBtton was clicked on.>>  
        Dim WhichRadioButton As RadioButton
        If TypeOf sender Is RadioButton Then
            WhichRadioButton = CType(sender, RadioButton)
            'Pass the number of the control to the PenWidth variable.>>  
            Integer.TryParse(WhichRadioButton.Name.Substring(5), PenWidth)
            'Then set the Pen Width.>>  
            MyPen.Width = PenWidth
        End If
    End Sub
    Private Sub picBoxOutput_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoxOutput.MouseDown
        'If the left mouse button is down the set IsThePenDown to TRUE  
        If e.Button = Windows.Forms.MouseButtons.Left Then
            IsThePenDown = True
            'Set the Point to start drawing from.>>  
            MouseDownPoint = New Point(e.X, e.Y)
        End If
    End Sub
    Private Sub picBoxOutput_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoxOutput.MouseUp
        'Set to FALSE on the MouseUp event.>>  
        IsThePenDown = False
    End Sub
    Private Sub picBoxOutput_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoxOutput.MouseMove
        'A STATIC variable holds or remebers the previous value.>>  
        Static LastPoint As New Point
        If IsThePenDown = True Then
            'Draw a line from the LastPoint.>>  
            picBoxOutput.CreateGraphics.DrawLine(MyPen, LastPoint.X, LastPoint.Y, e.X, e.Y)
        End If
        'Set the LastPoint to the previous point.>>  
        LastPoint = New Point(e.X, e.Y)
    End Sub
    Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
        MyPen.Color = Color.FromName(ToolStripComboBox1.SelectedItem.ToString)
        OptionsToolStripMenuItem.HideDropDown()
        cmbSelectColor.Text = ToolStripComboBox1.SelectedItem.ToString
    End Sub
    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        MyPen.Width = CSng(ToolStripMenuItem2.Text)
    End Sub
    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        MyPen.Width = CSng(ToolStripMenuItem3.Text)
    End Sub
End Class
 
I fixed it just doing this instead :

VB.NET:
    Private Sub rdoBtnWidth_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoBtnWidth1.CheckedChanged, rdoBtnWidth2.CheckedChanged, rdoBtnWidth3.CheckedChanged, rdoBtnWidth4.CheckedChanged
        Dim WhichRadioButton As RadioButton
        WhichRadioButton = CType(sender, RadioButton)
        MyPen.Width = CSng(WhichRadioButton.Text)
    End Sub
 
However there is one last thing I'm having a tough time with.

The width can be selected via the menustrip as well. When the PenWidth is set by the menustrip I need to have the same width radio button selected on Form1 as well.

VB.NET:
    Private Sub rdoBtnWidthMenuItem_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click, ToolStripMenuItem3.Click, ToolStripMenuItem4.Click, ToolStripMenuItem5.Click
        'Set the pen width to the selected width.>> 
        Dim WhichRadioButton As ToolStripItem
        WhichRadioButton = CType(sender, ToolStripItem)
        MyPen.Width = CSng(WhichRadioButton.Text)

    End Sub

Is there any easy way to create a collection or array of ToolStripMenuItem2,3,4,5 so that the appropriate one based off what was choosen in rdoBtnWidthMenuItem_click is also selected in rdoBtnWidth1.CheckedChanged rdoBtnWidth2.CheckedChanged ..etc ? Or better yet maybe just a select case ?
 
Last edited:
You need me to provide an example of assigning a value to a property and getting a value from a property?
VB.NET:
someToolStripMenuItem.Tag = someRadioButton
VB.NET:
DirectCast(someToolStripItem.Tag, RadioButton).Checked = True
While DirectCast is better, CType would have done the job too, and you already know how to use it.
 
My open problem with that is how to uncheck one if the other is selected

VB.NET:
        ToolStripMenuItem2.Tag = rdoBtnWidth1
        ToolStripMenuItem3.Tag = rdoBtnWidth2
        ToolStripMenuItem4.Tag = rdoBtnWidth3
        ToolStripMenuItem5.Tag = rdoBtnWidth4
        DirectCast(ToolStripMenuItem2.Tag, RadioButton).Checked = True
        DirectCast(ToolStripMenuItem3.Tag, RadioButton).Checked = True
        DirectCast(ToolStripMenuItem4.Tag, RadioButton).Checked = True
        DirectCast(ToolStripMenuItem5.Tag, RadioButton).Checked = True
 
You don't have to uncheck anything. The whole point of RadioButtons is that only one in a set can be checked at any one time. As long as your RadioButtons are all in the same container, checking one will automatically uncheck the others.
 
What its doing is sticking. If I select say 5 in the toolstrip it will select 5 on the radiobutton but then nothing else I select in the toolstrip will change the radiobutton, it stays stuck on the first selection 5.
 
If you're using the code from post #7 then it's no surprise because you're checking four different RadioButtons. Each time you check one the others get unchecked so only the last one you check stays checked. What you are supposed to do is ste the Tag property of each menu item once and once only, when the application starts up. In the event handler for the Click events of the menu items you get the Tag of the sender, which is the associated RadioButton, and check only that one RadioButton.
 
Back
Top