ARTICLE

Handling Mouse Events in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 June 11, 2003
Handling events in VB is little bit tricky than in C++ or C#. In VB.NET, you write a delegate and then write an event handler. These event handlers are overridable public events defined in the Control or other WinForms classes.
 
Reader Level:

Handling events in VB is little bit tricky than in C++ or C#. In VB.NET, you write a delegate and then write an event handler.  These event handlers are overridable public events defined in the Control or other WinForms classes.

1. Write a Delegate

I want to handle Mouse Down events and do something when left or right mouse buttons are pressed. Write this line of code in your InitializeComponent function.

AddHandler Me.MouseDown, AddressOf Me.Form_MouseDown

2. Write the Event

Now you write the event handle. The output parameter of your event returns System.WinForms.MouseEventArgs object which gives you the details about mouse down such as what button is pressed or how many times. Here are MouseEventArgs members.

MouseEventArgs members

Button

Indicates which mouse button was pressed. It could be Left, Right, Middle, or None.

Clicks

Indicates the number of times the mouse button was pressed and released.

Delta

Indicates a signed count of the number of detents the mouse wheel has rotated.

X

The x-coordinate of mouse click.

Y

The y-coordinate of mouse click.

 

 

 


 

 

Event Handler

Private Sub Form_MouseDown(ByVal sender As Object, ByVal e As System.WinForms.MouseEventArgs)
 Select Case e.Button
   Case MouseButtons.Left
     MessageBox.Show(
Me, "Left Button Click")
   Case MouseButtons.Right
      MessageBox.Show(
Me, "Right Button Click")
   Case MouseButtons.Middle
   Case Else
 End Select
End
Sub 'Form_MouseDown

share this article :
post comment
 

I'm working on a application and i want it to control my mouse as I want. I have all the code to make my mouse move somewhere on my screen, and to do a left click. Now all what i need is the code to make my mouse do a right click. If you could help me out it would be appreciated!

Posted by Robert Gagnon Ranger Apr 12, 2010

I would like to totally disable mouse and keyboard for 20 seconds. Just for fun, to make joke with my friend! I use vb2008 but i don't know how to do this! So please help! THX

Posted by Vladimir Jevtic Apr 08, 2010

The way you can do is, get your data from Excel to a DataSet and then save data to Access database. See Office Interop section of this site or C# Corner.

Posted by Mahesh Chand Aug 27, 2007

Mr Chand, Can you show me how to export Excel files into Access? Thanks, T.

Posted by toufik Aug 24, 2007

Fixed it. Thanks.

Posted by Mahesh Chand Aug 23, 2007
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor