Creating a Drawing Panel in VB.NET

Posted on 2006-07-18
Medium Priority
Last Modified: 2008-01-09
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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.BackColor = System.Drawing.SystemColors.ScrollBar
        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"
        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"
        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"
        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"
        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"
        Me.GroupBox2.BackColor = System.Drawing.SystemColors.ScrollBar
        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"
        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"
        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"
        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"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.ClientSize = New System.Drawing.Size(536, 310)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
        Me.Name = "FrmPainter"
        Me.Text = "Drawing Panel"

    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
Question by:danielmalek
  • 2
LVL 14

Expert Comment

ID: 17135201
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.

Author Comment

ID: 17135219
If you look at the above code, I have used radio buttons but am having difficulties creating the logic...
LVL 14

Accepted Solution

jjardine earned 1000 total points
ID: 17135311
How about if you added a Click event for the radio buttons.  Then when that event fires you can update your local variables.  For Example

Public Sub rdoRed_Click(sender as Object, e as eventargs) handles rdoRed.Click
  If rdoRed.checked Then
    _selectedColor = Color.Red
  End If
End Sub
LVL 21

Assisted Solution

oleggold earned 1000 total points
ID: 17135520
Here's the wonderfull step by step example by SAMS:
I'm sure You'll find all Your answers down there,e,g:
Draw module:
 Public Sub draw( _
    ByVal shape As String, _
    ByVal width As Integer, _
    ByVal height As Integer, _
    ByVal x As Integer, _
    ByVal y As Integer, _
    ByVal fillType As String, _
    ByVal outlineWeight As Single, _
    ByVal outlineColor As String, _
    ByVal solidColor As String, _
    ByVal blendFrom As String, _
    ByVal blendTo As String, _
    ByVal pattern As String, _
    ByVal patternForeColor As String, _
    ByVal patternBackColor As String, _
    ByVal blendStyle As String, _
    ByVal rotateAngle As Single)

    'purpose: draw a shape to the drawing surface (frmChild)

    'local scope
    Dim myPen As Pen
    Dim myRectangle As Rectangle
    Dim myBrush As Brush
    Dim myHatchStyle As Drawing2D.HatchStyle
    Dim myType As System.Type
    Dim myBlendStyle As Drawing2D.LinearGradientMode
    Dim myState As Drawing2D.GraphicsState

    'get the current state of the graphics object (pre-tranform)
    myState = m_myGraphics.Save()

    'set the rotation transformation value

    'check what shape to draw
    Select Case shape
      Case "Line"

        'create a new pen instance
        myPen = New Pen(color.FromName(name:=outlineColor), _

        'draw the line to the surface
        m_myGraphics.DrawLine(pen:=myPen, x1:=x, y1:=y, _
          x2:=x + width, y2:=y + height)

      Case "Rectangle", "Ellipse"

        'note: the rectangle and ellipse are very similar
        '   they can use the same code

        'create a new pen myect for the outline of the shape
        myPen = New Pen(color.FromName(name:=outlineColor), _

        'create the rectangle object
        myRectangle = New Rectangle(x:=x, y:=y, width:=width, _

        'draw the rectangle to the surface
        If shape = "Ellipse" Then

          'draw the ellipse
          m_myGraphics.DrawEllipse(pen:=myPen, _


          'draw a rectangle
          m_myGraphics.DrawRectangle(pen:=myPen, _

        End If

        'fill the rectangle/ellipse
        If fillType <> "NONE" Then

          'determine brush type to create
          Select Case fillType
            Case "SOLID"

              'create a new solid brush
              myBrush = New SolidBrush( _

            Case "PATTERN"

              'create the hatch brush
              'note: we use the type object and the parse
              '    method of the enum to return the
              '    value of the enum's member from its
              '    name (string)
              myType = myHatchStyle.GetType()
              myBrush = New System.Drawing.Drawing2D.HatchBrush _
              (myHatchStyle.Parse(enumType:=myType, _

            Case "BLEND"

              'create a blend brush
              'note: we use the type object and the parse
              '    method of the enum to return the
              '    value of the enum's member from its
              '    name (string)
              myType = myBlendStyle.GetType
              myBrush = New _
                System.Drawing.Drawing2D.LinearGradientBrush( _
                rect:=myRectangle, _
                color1:=color.FromName(name:=blendFrom), _
                color2:=color.FromName(name:=blendTo), _
                LinearGradientMode:=myBlendStyle.Parse( _
                enumType:=myType, value:=blendStyle))

          End Select

          'fill the shape
          If shape = "Ellipse" Then

            'draw the ellipse with the correct brush
            m_myGraphics.FillEllipse(brush:=myBrush, _


            'fill the rectangle with the correct brush
            m_myGraphics.FillRectangle(brush:=myBrush, _

          End If

        End If

    End Select

    'reset the state of the graphics object

  End Sub

End Module

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question