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 draw a line on Form which follows your mouse in VB.Net using System.Drawing.Graphics

Author: DevASP
Download Source Code : 481_Graphics.zip

In this simple article you will learn that how you can draw a line on Form which follows your mouse.

Simply just create New Project of windows application in VB and drag two labels and a timer on form.

 

Is create some global variables:

 

Dim myPen As Pen

Dim lnwid As Integer = 2

Dim gr As System.Drawing.Graphics

 

Now write code in following events:

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Me.BackColor = Color.LightGoldenrodYellow

        Me.WindowState = FormWindowState.Maximized

        myPen = New Pen(Color.Blue, 2)

        Me.Text = "DevAsp"

        Label1.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 50)

        Label2.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 150)

 

 

End Sub

 

 

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

 

        gr = e.Graphics

        gr.DrawLine(myPen, 650, 450, Me.MousePosition.X, Me.MousePosition.Y)

        tmr1.Start()

 

End Sub

 

 

Private Sub tmr1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr1.Tick

 

        lnwid += 1

        Me.BackColor = Color.LightGray

        Me.BackColor = Color.LightGreen

        Me.BackColor = Color.LightGray

 

        If check > 0 Then

            myPen = New Pen(Color.AliceBlue, lnwid)

        Else

            myPen = New Pen(Color.LightBlue, lnwid)

        End If

 

        Me.Validate()

        If lnwid > 30 Then

            lnwid = 0

            check = -check

        End If

 

End Sub

 

 

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

 

        If e.Button = MouseButtons.Left Then

            tmr1.Stop()

        End If

        If e.Button = MouseButtons.Right Then

            tmr1.Start()

        End If

 

End Sub

Article Comments
I want to know how to draw on the form thru the mouse.

Posted on 8/24/2007 12:38:56 AM by GajalakshmiB

sir,
i wanna draw a lines of variable size line which can be drawn on form,which can be deleted , and resizeable wit mouse

Posted on 9/14/2008 7:43:27 AM by praneeth

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to change background and fore ground color of the text in console application using VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net