Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to do drawing on form using mouse in VB.net - C #.net.

Author: Maliha Atteeq
Download Source Code : 1082_code.zip

In this simple article you will learn that how to draw on the form using mouse in VB and C#.net.

 Mouse events:

In order to draw with the help of your mouse, you have to know about the device and their events. There are certain events associated with the mouse, depending on your requirements we can use them. The most common event of mouse is click and double clicks. For drawing with the mouse you don’t have to bother about these two events. For drawing purpose we concern with the dragging movement of the mouse.

In this article I will tell you that how we can draw on a form with the help of your mouse.

Drag and draw:

In order to draw with mouse when it drags on the form you have to use its move, up and down events. All these events are defined; you just implement your logic behind them.

For the mouse down event, consider this fragment of code:

C#:

private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            shouldPaint = true;

        }

VB:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

    shouldPaint = True

End Sub

For the mouse up event, consider this fragment of code:

C#:

private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            shouldPaint = true;

        }

VB:

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)

    shouldPaint = True

End Sub

To demonstrate make a new window application and write the following code on mouse’s move event:

C#:

  private void Form1_MouseMove(object sender, MouseEventArgs e)

        {

            if (shouldPaint)

            {

                Graphics graphics = CreateGraphics();

                graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);

                graphics.Dispose();

            }

        }

                 

VB:

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

    If shouldPaint Then

        Dim graphics As Graphics = CreateGraphics()

        graphics.FillEllipse(New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4)

        graphics.Dispose()

    End If

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP APPLICATION";

       }

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

    Me.Text = "DEVASP APPLICATION"

    End Sub 

This simple article tells that how to draw on the form using mouse in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use hot key prefix in VB - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net