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 Using Visual Basic.Net

Author: DevASP
Download Source Code : 490_AllaboutMouse.zip

In this article I will try to explain how you can handle different mouse events, Swap mouse buttons, Set or get Double click time and how to change the mouse cursor for particular control.

This article is all about that how we can use the mouse events, Swap the mouse buttons, Set or get the Double click time and how to change the mouse cursor fro particular control

 

Create a new vb.net application and drag the following control on the from:

  • 40 buttons
  • 4 labels
  • 2 numeric up down control
  • 1 timer control

Import the following:

 

Imports System.Runtime.InteropServices

Before start working with events declare the following global variables:

Private i As Integer = 0

Private checkSMB As Boolean = False

 

Declare the following api to interact with mouse

 

Declare Function SwapMouseButton Lib "user32.dll" _

Alias "SwapMouseButton" (ByVal bSwap As Integer) As Integer

 

Declare Function GetDoubleClickTime Lib "user32.dll" _

Alias "GetDoubleClickTime" () As Integer

 

Declare Function SetDoubleClickTime Lib "user32.dll" _

Alias "SetDoubleClickTime" (ByVal uInterval As Integer) As Boolean

 

Now write the following code on swap button click event:

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SwapMouseButton(1)

MsgBox("Mouse Button Swap Press Right Click to OK", MsgBoxStyle.Information, "All About Mouse")

End Sub

 

Write the following code on reset button click event:

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

SwapMouseButton(0)

MsgBox("Mouse Button Reset Press Left Click to OK", MsgBoxStyle.Information, "All About Mouse")

End Sub

 

Write the following code on the form mouse move event to capture the mouse pointer position:

 

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

Label4.Text = e.X

Label3.Text = e.Y

End Sub

 

Write the following code on the form mouse Wheel event to get information that in which direction the mouse wheel move:

 

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

 

If e.Delta < 0 Then

            Button6.Enabled = True

            Button7.Enabled = False

      End If

      If e.Delta > 0 Then

Button7.Enabled = True

            Button6.Enabled = False

End If

 

End Sub

Write the from1 mouse down and from1 mouse up event to get information that which mouse button is pressed:

 

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

 

        If e.Button = MouseButtons.Left Then

            Button5.Enabled = True

        ElseIf e.Button = MouseButtons.Right Then

            Button3.Enabled = True

        ElseIf e.Button = MouseButtons.Middle Then

            Button4.Enabled = True

        End If

 

End Sub

 

 

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

 

        If e.Button = MouseButtons.Left Then

            Button5.Enabled = False

        ElseIf e.Button = MouseButtons.Right Then

            Button3.Enabled = False

        ElseIf e.Button = MouseButtons.Middle Then

            Button4.Enabled = False

        End If

 

End Sub

 

Write the following coed on get button click event to get the mouse double click time:

 

Private Sub Button41_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button41.Click

NumericUpDown2.Text = (GetDoubleClickTime())

End Sub

 

Write the following coed on Set button click event to set the mouse double click time:

 

Private Sub Button40_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button40.Click

SetDoubleClickTime(NumericUpDown1.Text)

End Sub

 

Now write the following code on Hide button click event to hide the mouse button on the form:

 

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

System.Windows.Forms.Cursor.Hide()

End Sub

 

 

Now write the following code on show button click event to show the mouse button on the form:

 

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

System.Windows.Forms.Cursor.Show()

End Sub

 

Double click on the timer control and write the following code on timer tick event to give some animation to mouse:

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If i = 0 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanNorth

            i = 1

ElseIf i = 1 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanNE

            i = 2

ElseIf i = 2 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanEast

            i = 3

ElseIf i = 3 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSE

            i = 4

ElseIf i = 4 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSouth

            i = 5

ElseIf i = 5 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSW

            i = 6

ElseIf i = 6 Then

            GroupBox1.Cursor = System.Windows.Forms.Cursors.PanWest

            i = 0

End If

End Sub

 

On start button click event write the following code

 

Private Sub Button37_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button37.Click

Timer1.Enabled = True

End Sub

 

On stop button click event write the following code

 

Private Sub Button38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button38.Click

      Timer1.Enabled = False

GroupBox1.Cursor = System.Windows.Forms.Cursors.Default

End Sub

 

Now how to change the mouse cursor, VB.net includes following cursors 

 

ARROW                CROSS                 DEFAULT

HAND                    HELP                   HSPLIT

IBEAM                   NO                       NOMOVE2D

NOMOVEHORIZ     NOMOVEVET       PANEAST

PANNE                  PANNORTH          PANNW

PANSE                  PANSOUTH          PANSW

PANWEST             SIZEALL               SIZENESW

SIZENS                  SIZENWSE           SIZEWE

UPARROW             VSPLIT                 WAITCURSOR

 

Now write the following code on ARROW button click event

 

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

GroupBox1.Cursor = System.Windows.Forms.Cursors.Arrow

End Sub

 

Now write the following code on CROSS click event

 

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.Cross

End Sub

 

Now write the following code on DEFAULT click event

 

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.Default

End Sub

 

Now write the following code on HAND click event

 

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.Hand

End Sub

 

Now write the following code on HELP click event

 

Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.Help

End Sub

 

Now write the following code on HSPLIT click event

 

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.HSplit

End Sub

 

Now write the following code on IBEAM click event

 

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.IBeam

End Sub          

 

 

Now write the following code on NO click event

 

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.No

End Sub

 

Now write the following code on NOMOVE2D click event

 

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.NoMove2D

End Sub

 

Now write the following code on NOMOVEHORIZ click event

 

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.NoMoveHoriz

End Sub

 

Now write the following code on NOMOVEVET click event

 

Private Sub Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button28.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.NoMoveVert

End Sub

 

Now write the following code on PANEAST click event

 

Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanEast

End Sub

 

Now write the following code on PANNE click event    

 

Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanNE

End Sub

 

Now write the following code on PANNORTH click event 

 

Private Sub Button29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button29.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanNorth

End Sub

 

Now write the following code on PANNW click event

 

Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanNW

End Sub

 

Now write the following code on PANSE click event

 

Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSE

End Sub

 

Now write the following code on PANSOUTH click event 

 

Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSouth

End Sub

 

Now write the following code on PANSW click event

 

Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanSW

End Sub

 

Now write the following code on PANWEST click event  

 

Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.PanWest

End Sub

 

Now write the following code on SIZEALL click event  

 

Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.SizeAll

End Sub

 

Now write the following code on SIZENESW click event

 

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.SizeNESW

End Sub

 

Now write the following code on SIZENS click event

 

Private Sub Button32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button32.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.SizeNS

End Sub

 

Now write the following code on SIZENWSE click event 

 

Private Sub Button31_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button31.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.SizeNWSE

End Sub

 

Now write the following code on SIZEWE click event

 

Private Sub Button34_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button34.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.SizeWE

End Sub

 

Now write the following code on UPARROW click event  

 

Private Sub Button30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button30.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.UpArrow

End Sub

 

Now write the following code on VSPLIT click event   

 

Private Sub Button36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button36.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.VSplit

End Sub

 

Now write the following code on WAITCURSOR click event

 

Private Sub Button35_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button35.Click

        GroupBox1.Cursor = System.Windows.Forms.Cursors.WaitCursor

End Sub

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Drawing a Rectangle on Windows Form Using System.Drawing.Graphics

Disclaimer - Privacy
© 2002-2017 DevASP.net