Event and Event Handler in Visual Basic.Net

Event and Event Handler in Visual Basic.Net

The programs written in Visual Basic.Net are basically event oriented programs. In other words; events determine the flow of the program. So events are of great importance in Visual Basic.Net. What are these events?


Event and Event Handler

Event is basically an action by the user like clicking the mouse, moving the mouse, pressing any key on the keyboard and etc. or any signal or notification from the system to the program. Just like in calculators that you press different keys to perform an arithmetic operation, you create events for the calculator. Calculator, being an application, has to respond to your created events.

When any event is occurred or detected in any program, a code corresponding to that event has to be executed. That piece of code is called to be the Even Handler.

Events can basically come from Mouse, Keyboard or system.

Event Example Code

Below is a simple example of a Click-Event from the mouse;

Code:




Output:

Output 1

In the example above, you see the following line of code.

It is a private sub by the name of Button1_Click. It takes two arguments and this sub handles the click event for the Button1. The rest of the code inside the sub is called to be the event handler.

Every object in a VB.Net program has a default event. For example; the default event for a form is load. That is the reason when you create a form and double click over it, it automatically creates a sub for controlling the load event for the form. Moreover; the default event for a button is a click event. That’s why in the above example you see the click event for the button (Sum) we created.

Related: Mathematical Function in Visual Basic.Net

You can change the events for an object in VB.Net. In the code window, top right you can find a drop down menu. Once you click it, you can find other events that can occur through an object.

Event Menu

In the example below, we have created 4 different events. The first is Click_Event for Button1. The second is ForeColorChanged_Event for Label1. The third is MouseHover_Event for Button2. And the last is MouseLeave_Event for Button2.

Code:

Output:




Some Popular Events

Check Also: How to create a Dictionary in Microsoft Excel

Some of the most popular events that you can use while writing a program can be:

MouseClick: It occurs when the control is clicked by the mouse.

MouseHover: It occurs when the mouse hovers over the control.

MouseLeave: It occurs when the mouse leaves the control.

MouseWheel: It occurs when the control has the focus and the mouse wheel moves.

KeyPress: It occurs when the control has the focus and a key is pressed on the keyboard.

Enter: It occurs when the control has the focus and the enter key is pressed on the keyboard.

Last but not the least, it is important to know that events can also be created. These type of events are called to be Custom Events. Custom Events is to be discussed in a separate article.

Previous How to Create a Dictionary in Microsoft Excel
Next Working with Multiple Forms in Visual Basic. Net

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like