Link to home
Start Free TrialLog in
Avatar of danielmalek
danielmalek

asked on

Creating a Drawing Panel in VB.NET

Hello All,

I am working on creating a mini-paint program written in VB.NET.  The program is relatively simple in nature, my issue is this...

While I can get the program to paint fine, I now want to add functionality to select one of four different paint colors (Red, Blue, Green, or Black) and one of three bursh sizes (Small, Medium, or Large).

I am completely versed in the stance of homework in Experts-Exchange and am not seeking someone to give me the correct code, instead some clear explanations and guidance would be most appreciated.  My code will be posted next...

==================================================================================================

Public Class FrmPainter
    Inherits System.Windows.Forms.Form

    Dim shouldPaint As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents btnBlack As System.Windows.Forms.RadioButton
    Friend WithEvents btnGreen As System.Windows.Forms.RadioButton
    Friend WithEvents btnBlue As System.Windows.Forms.RadioButton
    Friend WithEvents btnRed As System.Windows.Forms.RadioButton
    Friend WithEvents btnLarge As System.Windows.Forms.RadioButton
    Friend WithEvents btnMedium As System.Windows.Forms.RadioButton
    Friend WithEvents btnSmall As System.Windows.Forms.RadioButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.btnBlack = New System.Windows.Forms.RadioButton
        Me.btnGreen = New System.Windows.Forms.RadioButton
        Me.btnBlue = New System.Windows.Forms.RadioButton
        Me.btnRed = New System.Windows.Forms.RadioButton
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.btnLarge = New System.Windows.Forms.RadioButton
        Me.btnMedium = New System.Windows.Forms.RadioButton
        Me.btnSmall = New System.Windows.Forms.RadioButton
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.BackColor = System.Drawing.SystemColors.ScrollBar
        Me.GroupBox1.Controls.Add(Me.btnBlack)
        Me.GroupBox1.Controls.Add(Me.btnGreen)
        Me.GroupBox1.Controls.Add(Me.btnBlue)
        Me.GroupBox1.Controls.Add(Me.btnRed)
        Me.GroupBox1.Location = New System.Drawing.Point(0, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(112, 168)
        Me.GroupBox1.TabIndex = 1
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Color"
        '
        'btnBlack
        '
        Me.btnBlack.Location = New System.Drawing.Point(24, 120)
        Me.btnBlack.Name = "btnBlack"
        Me.btnBlack.Size = New System.Drawing.Size(64, 24)
        Me.btnBlack.TabIndex = 3
        Me.btnBlack.Text = "Black"
        '
        'btnGreen
        '
        Me.btnGreen.Location = New System.Drawing.Point(24, 88)
        Me.btnGreen.Name = "btnGreen"
        Me.btnGreen.Size = New System.Drawing.Size(64, 24)
        Me.btnGreen.TabIndex = 2
        Me.btnGreen.Text = "Green"
        '
        'btnBlue
        '
        Me.btnBlue.Location = New System.Drawing.Point(24, 56)
        Me.btnBlue.Name = "btnBlue"
        Me.btnBlue.Size = New System.Drawing.Size(64, 24)
        Me.btnBlue.TabIndex = 1
        Me.btnBlue.Text = "Blue"
        '
        'btnRed
        '
        Me.btnRed.Location = New System.Drawing.Point(24, 24)
        Me.btnRed.Name = "btnRed"
        Me.btnRed.Size = New System.Drawing.Size(64, 24)
        Me.btnRed.TabIndex = 0
        Me.btnRed.Text = "Red"
        '
        'GroupBox2
        '
        Me.GroupBox2.BackColor = System.Drawing.SystemColors.ScrollBar
        Me.GroupBox2.Controls.Add(Me.btnLarge)
        Me.GroupBox2.Controls.Add(Me.btnMedium)
        Me.GroupBox2.Controls.Add(Me.btnSmall)
        Me.GroupBox2.Location = New System.Drawing.Point(0, 168)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(112, 144)
        Me.GroupBox2.TabIndex = 2
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Size"
        '
        'btnLarge
        '
        Me.btnLarge.Location = New System.Drawing.Point(24, 88)
        Me.btnLarge.Name = "btnLarge"
        Me.btnLarge.Size = New System.Drawing.Size(64, 24)
        Me.btnLarge.TabIndex = 6
        Me.btnLarge.Text = "Large"
        '
        'btnMedium
        '
        Me.btnMedium.Location = New System.Drawing.Point(24, 56)
        Me.btnMedium.Name = "btnMedium"
        Me.btnMedium.Size = New System.Drawing.Size(64, 24)
        Me.btnMedium.TabIndex = 5
        Me.btnMedium.Text = "Medium"
        '
        'btnSmall
        '
        Me.btnSmall.Location = New System.Drawing.Point(24, 24)
        Me.btnSmall.Name = "btnSmall"
        Me.btnSmall.Size = New System.Drawing.Size(64, 24)
        Me.btnSmall.TabIndex = 4
        Me.btnSmall.Text = "Small"
        '
        'FrmPainter
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.ClientSize = New System.Drawing.Size(536, 310)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
        Me.Name = "FrmPainter"
        Me.Text = "Drawing Panel"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' draw circle if shouldPaint is True
    Private Sub FrmPainter_MouseMove( _
                ByVal sender As System.Object, _
                ByVal e As System.Windows.Forms.MouseEventArgs) _
                Handles MyBase.MouseMove

        ' paint circle if mouse pressed
        If shouldPaint Then
            Dim graphic As Graphics = CreateGraphics()

            graphic.FillEllipse _
                (New SolidBrush(Color.Red), e.X, e.Y, 4, 4)
        End If

    End Sub 'FrmPainter_MouseMove

    ' set shouldPaint to True
    Private Sub FrmPainter_MouseDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseDown

        shouldPaint = True
    End Sub ' FrmPainter_MouseDown

    ' set shouldPaint to False
    Private Sub FrmPainter_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseUp

        shouldPaint = False
    End Sub ' FrmPainter_MouseUp
End Class

==================================================================================================

Any guidance is most appreciated.  What I think the next step would be is to create some type of case control repitition, but I am having difficulty determining how this would operate.

Kind Regards,

Dan Malek
danielmalek@hotmail.com
Avatar of jjardine
jjardine
Flag of United States of America image

I think you would start by adding in either a listbox, dropdownlist, or radio buttons that would allow the user to select which color and brush size they want to use.   Then Once they select that value, store that value in some variables (ie.   _selectedColor as Color and _selectedBrush as Brush)   So when they set their change their settings, do a case statement to map your strings in the list to the actual color to store it.  Then in your code change where you hard code the Color.red or any other reference to color to the new _selectedColor and the same with the Brush.
Avatar of danielmalek
danielmalek

ASKER

If you look at the above code, I have used radio buttons but am having difficulties creating the logic...
ASKER CERTIFIED SOLUTION
Avatar of jjardine
jjardine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial