Click and Mouse Hover Event in Visual Basic- In this article, I am going to show you how to use mouse hover in Visual basic, in our example should contain on the screen a text label and a Button. The label will display the text “Programming Digest”, and when you click on the command button, having ” Click me ” caption, a dialog box will open with the message” Hello Programming Digest! ” In addition, when the mouse pointer is hovering over the text label (the same the MouseHover event), a dialog with the text “Mouse Hover Event “.
Creating MouseHover project in Visual Basic:
Start Visual Studio 12, click on the file in the File menu click on new, in the new dropdown menu click on Project, or simply press Ctrl+Shift+N. In the New Project window that opens, in the left column, find the Visual item Basic, and then in the Templates area – in the middle column – select Windows Forms Application template. Write the project name MouseHover, and click on the OK button.
In the form designer, from the Toolbox panel, drag a Label and a button to the form I set the label name as lblshow, and for a button I set BtnClick, and then design form to your liking in my case I designed as shown below.
Now let’s add three event handlers to the program code. To do this, in the Properties panel, click on the lightning bolt (Events)
and double-click consecutively select:
form load event Form_Load;
event – click on the BtnClick_Click button;
This will switch to the tab of the program code Form1.vb, and the environment Visual Studio 12 will generate three empty event handlers. For example, the last event handler will look like this:
Private Sub lblShow_Click(sender As Object, e As EventArgs) Handles lblShow.MouseHover End Sub
Between these two lines, insert a call to the dialog box:
MessageBox.Show (" Mouse Hover Event")
Now let’s check the capabilities of the program – press the <F5> key, “hang” with the mouse pointer over Label1, click on the Button1 button. Everything is working!
So, back to the form load event – the control environment generated an empty handler for it:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub
Next, I would like to see the top left of the form on a blue background (in the so-called title bar) was not the Form1, but something meaningful. For example, the word ” Simple Mouse Hover Event” To do this, set this string to the Text property of the form below. To do this, set this string to the Text property of the form below. Because we change the property of the Form1 object inside the form-related event handler, you must access the form either through the Me reference or using the MyBase or MyClass keywords:
Me.Text = "Simple Mouse Hover Event"
Complete code of Visual basic Mouse Hover Event:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Text = "Simple Mouse Hover Event" End Sub Private Sub BtnClick_Click(sender As Object, e As EventArgs) Handles BtnClick.Click MessageBox.Show("Hello Programming Digest!") End Sub Private Sub lblShow_Click(sender As Object, e As EventArgs) Handles lblShow.MouseHover MessageBox.Show("Mouse Hover Event!") End Sub End Class
When you click on the button it will show the following output
And when you, hover the mouse pointer on the label it will show the following output: