Handling Mouse Events in Visual Basic .NET

In this article, I will explain you about Handling Mouse Events in Visual Basic .NET.
  • 15680

In this article, I will explain you about Handling Mouse Events in Visual Basic .NET.

Handling Mouse Events in Forms

In visual Basic .NET we can handle mouse events such as mouse pointer movements in Forms. Following table show you various mouse events in Visual Basic .NET.

Mouse Events

Occurrence

MouseDown

This event happens when the mouse pointer is over the form/control and is pressed.

MouseUp

This event happens when the mouse pointer is over the form/control and themouse button is released.

MouseMove

This event happens when the mouse pointer is moved over the form/control.

MouseWheel

This event happens when the mouse wheel moves while the form/control has focus.

MouseLeave

This event happens when the mouse pointer leaves the form/control.

MouseEnter

This event happens when the mouse pointer enters the form/control.

MouseHover

This event happens when the mouse pointer hovers over the form/control.


In this table, I show you the properties of the MouseEventArgs objects that can be passed to the mouse event handler.

Properties

Use

Button

Specifies that the mouse button was pressed.

Clicks

Specifies number of times the mouse button is pressed and released.

X

The X-coordinate of the mouse click.

Y

The Y-coordinate of the mouse click.

Delta

Specifies a count of the number of detents (rotation of mouse wheel) the mouse wheel has rotated.

Example:

To understand mouse events in a form let's do an example. Drag four textbox onto your formTextBox1TextBox2TextBox3 and TextBox4. Now take some mouse events like MouseEnter,MouseDownMouseLeave and MouseClick. We are display the results of mouse events in the TextBoxes. The code below will show you how to do that:

Public Class Form1
 
    Private Sub Form1_MouseEnter(ByVal sender As System.ObjectByVal e As System.EventArgs)

Handles MyBase.MouseEnter
        TextBox1.Text = "Mouse Entered"
        'displaying "mouse entered" in TextBox2 when the mouse pointer enters the form
    End Sub
 
    Private Sub Form1_MouseDown(ByVal sender As System.ObjectByVal e AsSystem.Windows.Forms.MouseEventArgs)

 Handles MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            TextBox2.Text = "Mouse Left click at" + CStr(e.X) + " :" + CStr(e.Y)
            'displaying the coordinates in TextBox1 when the mouse is pressed on the form
        End If
    End Sub
 
    Private Sub Form1_MouseLeave(ByVal sender As System.ObjectByVal e As System.EventArgs)

HandlesMyBase.MouseLeave
        TextBox3.Text = "Mouse Exited"
        'displaying "mouse exited" in Textbox3 when the mouse pointer leaves the form
    End Sub
 
    Private Sub Form1_MouseClick(ByVal sender As System.ObjectByVal e AsSystem.Windows.Forms.MouseEventArgs)

Handles MyBase.MouseClick
        If e.Button = MouseButtons.Right Then
            TextBox4.Text = "Mouse Right click" + CStr(e.Clicks) + "times."
            'Displaying number of times the mouse right button is pressed and released.
        End If
    End 
Sub
End Class

The image below displays the output:

img1.gif
 

Summary

I hope this article help you to understand about Handling Mouse Events in Visual Basic .NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.