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
 

Working with Mouse Events in VB.Net Application

Author: DevASP
Download Source Code : 337_Mouse_Controls.zip

In this simple article you will learn to work with Mouse events like Button Click check, Scroll Check, Mouse Location Track, check Mouse is inside a control or not.

  • Create New Project in VB.net. Drag labels on the form1. 
  • First label having text Mouse is not inside and having name lblinside.
  • Second label having name lblx and no text.
  • Third label having name lbly and also no text
  • Fourth fifth and sixth labels having text UnPressed and having name lblleft, lblright, lblmiddle.
  • And the last label having name lblScroll and having text Not Scrolled. 
  • Now write code on form load event

 

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

 

        lblleft.ForeColor = Color.Blue

        lblright.ForeColor = Color.Blue

        lblmiddle.ForeColor = Color.Blue

        LBLSCROLL.ForeColor = Color.Blue

        lblx.ForeColor = Color.Blue

        lbly.ForeColor = Color.Blue

        lblinside.ForeColor = Color.LightGray

 

End Sub

 

  • Now write code on mouse move event to get Mouse x-axis position and also y-axis.

 

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

 

        lblx.Text = e.X

        lbly.Text = e.Y

 

End Sub

 

  • Now write code on mouse down event of the form.

 

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

 

        If e.Button = MouseButtons.Left Then

            lblleft.Text = "Pressed"

 

        ElseIf e.Button = MouseButtons.Right Then

            lblright.Text = "Pressed"

 

        ElseIf e.Button=MouseButtons.Middle  Then

            lblmiddle.Text = "Pressed"

        End If

 

End Sub

  • and now write code for mouse Up Event

 

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

 

        If e.Button = MouseButtons.Left Then

            lblleft.Text = "UnPressed"

        End If

        If e.Button = MouseButtons.Right Then

            lblright.Text = "UnPressed"

        End If

        If e.Button = MouseButtons.Middle Then

            lblmiddle.Text = "UnPressed"

        End If

 

End Sub

  • Now write code on mouse enter, mouse leave mouse down, mouse up events of lblinside

 

Private Sub lblinside_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblinside.MouseEnter

 

        lblinside.Text = "Mouse Is Inside Now"

 

End Sub

 

Private Sub lblinside_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblinside.MouseLeave

 

        lblinside.Text = "Mouse Is Not Inside"

 

End Sub

 

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

 

        If e.Button = MouseButtons.Left Then

            lblleft.Text = "Pressed"

 

        ElseIf e.Button = MouseButtons.Right Then

            lblright.Text = "Pressed"

 

        ElseIf e.Button = MouseButtons.Middle Then

            lblmiddle.Text = "Pressed"

        End If

 

End Sub

 

Private Sub lblinside_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblinside.MouseUp

 

        If e.Button = MouseButtons.Left Then

            lblleft.Text = "UnPressed"

        End If

        If e.Button = MouseButtons.Right Then

            lblright.Text = "UnPressed"

        End If

        If e.Button = MouseButtons.Middle Then

            lblmiddle.Text = "UnPressed"

        End If

 

End Sub

 
  • and in last to check mouse scroll

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

 

        If e.Delta < 0 Then

            LBLSCROLL.Text = "DOWN DIRECTION"

        End If

        If e.Delta > 0 Then

            LBLSCROLL.Text = "UP DIRECTION"

        End If

 

End Sub

Article Comments
i got some use out of this code for my ref
can i get ur contact info it will be helpful if u do so
thanks

Regards,
subramanya.hp

Posted on 9/21/2006 1:57:36 AM by subramanya.hp

Thanks a million. Really helped me out on my code. I already had done the mouse position by hand, but this really helped me out, and I threw away those few lines of code for a simple event handler.
THANKS!

Posted on 3/31/2007 5:45:17 PM by Craig

how would i go about making my app track the mouse when outside of the form?

Posted on 5/13/2007 10:06:05 AM by jason

Mouse Leave & Mouse Enter events are automatically done so we hava not write code

Posted on 6/6/2007 2:04:59 AM by pinali

Just what I needed. I'm a newbe. selftaught, hard to find complete examples.

Posted on 2/15/2008 5:27:17 PM by Roger Blake

Thank X 1000 for Event handling Kindly can u send me keyboard handling code. and alo about object oriented programming in VB.Net

Posted on 5/20/2008 3:35:31 AM by Junaid

This Article was very nice and useful. I was tried this and use for my project.

Posted on 8/9/2008 6:55:36 AM by srinivasan

this article is very useful especially for those immigrants from VB6
thanks alot

Posted on 1/24/2010 6:48:40 AM by Omar

it is very help full for generate the mouseEvent in your project..

Posted on 3/8/2010 10:17:54 AM by sonu Ranjan

hey,nice one
if you have more than this send me on my mail id
thanks

Posted on 3/12/2010 6:26:00 AM by renuka

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Progress Bar Control in VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net