Visual Basic .NET 2003 Drawing Applications

Hello whoever,

I am making a basic drawing application for a class that consists of a simple form with

a pnl for the drawing area and radio buttons to modify the object size or color.

When I create the MouseEvents to draw with, generated handler is for the painter panel. For example,

....Handles pnlPainter.MouseMove

The problem is that when I do move the mouse, the application draws on the for instead. Apparently,

it does draw in the location respective to where I click on the panel, so 1,1 clicked on the panel draws

at 1, 1 on the form. I can make it draw on the form in the location clicked by changing the event handler

to MyBase.MouseMove

I will paste the code below, if someone has time to look it over. I have already submitted my work

and am more interested in knowing what the problem is than worrying about the grade, so delayed

replies are welcome.

Thanks and here is the code...

Public Class FrmAdvancedPainter

Inherits System.Windows.Forms.Form

' create Color object and initialize to Black

Private m_objBrushColor As Color = Color.Black

' specify whether application should paint

Private m_blnShouldPaint As Boolean = False

' specify whether application should erase

Private m_blnShouldErase As Boolean = False

' diameter of MouseDown circle (initially set to small)

Private m_intDiameter As Integer = 4

' enumeration for size options

Enum Sizes

Small = 4

Medium = 8

Large = 10

End Enum ' sizes

Private m_objGraphic As Graphics = CreateGraphics()

#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 fraColor As System.Windows.Forms.GroupBox

Friend WithEvents fraSize As System.Windows.Forms.GroupBox

Friend WithEvents radRed As System.Windows.Forms.RadioButton

Friend WithEvents radBlue As System.Windows.Forms.RadioButton

Friend WithEvents radGreen As System.Windows.Forms.RadioButton

Friend WithEvents radBlack As System.Windows.Forms.RadioButton

Friend WithEvents radSmall As System.Windows.Forms.RadioButton

Friend WithEvents radMedium As System.Windows.Forms.RadioButton

Friend WithEvents radLarge As System.Windows.Forms.RadioButton

Friend WithEvents pnlPainter As System.Windows.Forms.Panel

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.fraColor = New System.Windows.Forms.GroupBox

Me.radBlack = New System.Windows.Forms.RadioButton

Me.radGreen = New System.Windows.Forms.RadioButton

Me.radBlue = New System.Windows.Forms.RadioButton

Me.radRed = New System.Windows.Forms.RadioButton

Me.fraSize = New System.Windows.Forms.GroupBox

Me.radLarge = New System.Windows.Forms.RadioButton

Me.radMedium = New System.Windows.Forms.RadioButton

Me.radSmall = New System.Windows.Forms.RadioButton

Me.pnlPainter = New System.Windows.Forms.Panel

Me.fraColor.SuspendLayout()

Me.fraSize.SuspendLayout()

Me.SuspendLayout()

'

'fraColor

'

Me.fraColor.Controls.Add(Me.radBlack)

Me.fraColor.Controls.Add(Me.radGreen)

Me.fraColor.Controls.Add(Me.radBlue)

Me.fraColor.Controls.Add(Me.radRed)

Me.fraColor.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.fraColor.Location = New System.Drawing.Point(8, 8)

Me.fraColor.Name = "fraColor"

Me.fraColor.Size = New System.Drawing.Size(96, 152)

Me.fraColor.TabIndex = 0

Me.fraColor.TabStop = False

Me.fraColor.Text = "Color"

'

'radBlack

'

Me.radBlack.Checked = True

Me.radBlack.Location = New System.Drawing.Point(16, 120)

Me.radBlack.Name = "radBlack"

Me.radBlack.Size = New System.Drawing.Size(56, 24)

Me.radBlack.TabIndex = 3

Me.radBlack.TabStop = True

Me.radBlack.Text = "Black"

'

'radGreen

'

Me.radGreen.Location = New System.Drawing.Point(16, 88)

Me.radGreen.Name = "radGreen"

Me.radGreen.Size = New System.Drawing.Size(64, 24)

Me.radGreen.TabIndex = 2

Me.radGreen.Text = "Green"

'

'radBlue

'

Me.radBlue.Location = New System.Drawing.Point(16, 56)

Me.radBlue.Name = "radBlue"

Me.radBlue.Size = New System.Drawing.Size(56, 24)

Me.radBlue.TabIndex = 1

Me.radBlue.Text = "Blue"

'

'radRed

'

Me.radRed.Location = New System.Drawing.Point(16, 24)

Me.radRed.Name = "radRed"

Me.radRed.Size = New System.Drawing.Size(56, 24)

Me.radRed.TabIndex = 0

Me.radRed.Text = "Red"

'

'fraSize

'

Me.fraSize.Controls.Add(Me.radLarge)

Me.fraSize.Controls.Add(Me.radMedium)

Me.fraSize.Controls.Add(Me.radSmall)

Me.fraSize.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.fraSize.Location = New System.Drawing.Point(8, 168)

Me.fraSize.Name = "fraSize"

Me.fraSize.Size = New System.Drawing.Size(96, 112)

Me.fraSize.TabIndex = 0

Me.fraSize.TabStop = False

Me.fraSize.Text = "Size"

'

'radLarge

'

Me.radLarge.Location = New System.Drawing.Point(16, 80)

Me.radLarge.Name = "radLarge"

Me.radLarge.Size = New System.Drawing.Size(64, 24)

Me.radLarge.TabIndex = 2

Me.radLarge.Text = "Large"

'

'radMedium

'

Me.radMedium.Location = New System.Drawing.Point(16, 56)

Me.radMedium.Name = "radMedium"

Me.radMedium.Size = New System.Drawing.Size(72, 16)

Me.radMedium.TabIndex = 1

Me.radMedium.Text = "Medium"

'

'radSmall

'

Me.radSmall.Checked = True

Me.radSmall.Location = New System.Drawing.Point(16, 24)

Me.radSmall.Name = "radSmall"

Me.radSmall.Size = New System.Drawing.Size(64, 24)

Me.radSmall.TabIndex = 0

Me.radSmall.TabStop = True

Me.radSmall.Text = "Small"

'

'pnlPainter

'

Me.pnlPainter.BackColor = System.Drawing.Color.White

Me.pnlPainter.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.pnlPainter.Location = New System.Drawing.Point(112, 8)

Me.pnlPainter.Name = "pnlPainter"

Me.pnlPainter.Size = New System.Drawing.Size(336, 272)

Me.pnlPainter.TabIndex = 1

'

'FrmAdvancedPainter

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)

Me.ClientSize = New System.Drawing.Size(456, 285)

Me.Controls.Add(Me.pnlPainter)

Me.Controls.Add(Me.fraColor)

Me.Controls.Add(Me.fraSize)

Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Name = "FrmAdvancedPainter"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "Advanced Painter"

Me.fraColor.ResumeLayout(False)

Me.fraSize.ResumeLayout(False)

Me.ResumeLayout(False)

End Sub

#End Region

' change color to red

Private Sub radRed_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radRed.CheckedChanged

m_objBrushColor = Color.Red

End Sub ' red radio click event

' change color to blue

Private Sub radBlue_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radBlue.CheckedChanged

m_objBrushColor = Color.Blue

End Sub ' blue radio click event

' change color to green

Private Sub radGreen_CheckedChanged(ByVal sender _

As System.Object, ByVal e As System.EventArgs) _

Handles radGreen.CheckedChanged

m_objBrushColor = Color.Green

End Sub ' green radio click event

' change color to black

Private Sub radBlack_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radBlack.CheckedChanged

m_objBrushColor = Color.Black

End Sub ' black radio click event

' change object size to small

Private Sub radSmall_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radSmall.CheckedChanged

m_intDiameter = Sizes.Small

End Sub ' small diameter radio event

' change object size to medium

Private Sub radMedium_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radMedium.CheckedChanged

m_intDiameter = Sizes.Medium

End Sub ' medium diameter radio event

' change object to large

Private Sub radLarge_CheckedChanged(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles radLarge.CheckedChanged

m_intDiameter = Sizes.Large

End Sub ' large diameter radio event

' handles pnlPainter mouse down event

Private Sub pnlPainter_MouseDown(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles pnlPainter.MouseDown

' draw on Form if the left button is held down

If e.Button = MouseButtons.Left Then

m_blnShouldPaint = True

' erase if right button is held down

ElseIf e.Button = MouseButtons.Right Then

m_blnShouldErase = True

End If

End Sub ' pnlPainter mouse down event

' handles FrmPainter's MouseUp event

Private Sub pnlPainter_MouseUp(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles pnlPainter.MouseUp

' do not erase or draw on form

m_blnShouldPaint = False

m_blnShouldErase = False

End Sub ' FrmPainter's MouseUp event

' handles pnlPainter's MouseMove event

Private Sub pnlPainter_MouseMove(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles pnlPainter.MouseMove

' draw circle if left mouse button is pressed

If m_blnShouldPaint = True Then

m_objGraphic.FillEllipse(New SolidBrush(m_objBrushColor), _

e.X, e.Y, m_intDiameter, m_intDiameter)

ElseIf m_blnShouldErase = True Then

m_objGraphic.FillEllipse(New SolidBrush(BackColor), _

e.X, e.Y, m_intDiameter, m_intDiameter)

End If

End Sub ' pnlPainter's MouseMove event

End Class ' FrmAdvancedPainter




Answer this question

Visual Basic .NET 2003 Drawing Applications

  • Pierre Bakker

    Thank you for your answer. changed the x, y coordinated to match the form location (e.X + 112), (e.Y + 8). It seems to be drawing behind the panel on the form. Is there a property setting that would allow me to draw on the panel

    Also, how do I adjust the drawing boundaries.

    Thank you very much for your help,



  • chinta

    Disregard the boundaries.Wouldn't they be defined by the control that raises the event In this case they were defined by the pnlPainter's MouseMove event.

    Directing them on to the form is simpler to me as well. In fact, I turned in a separate assignment showing that I understood how to draw and change the colors and sizes. In that assignment, I drew on the form.

    I am not sure I understand what you mean about the panel's paint event or how to handle that to draw on the panel. I thought using the panels mouse events for creategraphics would put it there.

    Thanks again,

    Gary

     



  • U412

    I'm sorry, but your whole approach is wrong. Run your app, draw something, then open calculator and drag it over your window. What you drew will disappear. To draw something properly, it needs to occur in your paint event. CreateGraphics is good for drawing things you *want* to disappear, like rubber banding.



  • RBear3

    The co-ordinates you recieve will be relative to the control that catches the message. To make it relative to the form, just add the panels Left and Top properties to the values that you're being passed in the panel.



  • fkmfkm

    I found the answer for anyone who comes across a similar problem. The create graphics must be includes in the mouse move event for the control that it should affect. Taking the following line,

    Private m_objGraphic As Graphics = pnlPainter.CreateGraphics()

    ...and changing "Private" to "Dim" allowed me to put this code in the MouseMove Event for the pnlPainter and fixed everything.

    Thanks for the help...



  • Bill Tang at ut

    Ah - well, if you want to draw on the panel, you need to handle it's paint event, not the event of the form. This would also negate what we did before, your co-ordinates would then be correct.

    What do you mean by 'drawing boundaries'

    I personally would not use a panel, I'd draw direct onto the form.



  • mikebz

    In fact, you should never call creategraphics. You should handle the Paint event and put your drawing code in there. If you call creategraphics, the things you draw will disappear when you hide and show your form again.



  • Visual Basic .NET 2003 Drawing Applications