Understanding Events in VB.NET

This tutorial will walk you through the basics of object-oriented events programming in VB.Net.

Definition of Events

Think of events as triggering mechanisms you use to accomplish a task. For example, to turn on the lights in your room you need to press the switch on button. Thus you need the “switch on” event triggered so that your lights will turn on.

In VB.NET, the same definition applies. Events are special types of methods (also known as functions) used by objects to interact with users, operating systems and even to another object.  One of the most common types of event triggering mechanisms comes from the user of a form. For example, there are several ways a user can interact with your VB.NET project that can be considered events:

a.) Moving his mouse cursor over your project.
b.) Clicking buttons
c.) Mousing over any objects in your application
d.) Pressing the left click or right click on the mouse.
e.) Changing text in your VB.NET textbox.

And there can be more. The above examples come from a user. These are called “user-triggered event mechanisms”. There are also events triggered from the Windows operating system, such as writing applications that interact with different operating system processes. There are also events triggered from other VB.NET objects.

The “Click event” Example

Now that you know some basic concepts, let’s create an example project that illustrates one of the most commonly used events in VB.NET- the click event. Follow the steps below:

1.) Launch Microsoft Visual Basic Express.
2.) Go to File – New Project.
3.) Under “Visual Studio Installed Templates”. Select “Windows Forms Application”.
4.) Name the project “EventExample” and click OK.
5.) You will then see “Form1”, which is a form control.
6.) In the toolbox, click and drag one TextBox control and put it in the center of the form.
7.) Click and drag one Button control and place it below the Textbox.
8.) Click and drag the Label control and put it above the Textbox. (This is Label1.)
9.) Click and drag the Label control below the button. (This is Label2.)
10.) Change the text value of the label from “Label1” to “Enter your name in the textbox:”
11.) Change the text value of the button from “Button1” to “Submit”.
12.) Finally change the value of the label from “Label2” to “Your name will show here”. This is how your VB.NET project should look in the Designer:

What does this do? First, it lets the user enter the name in the textbox and then click submit. After submitting the name, the name will show back in the form replacing “Your name will be shown here”.

How to Access the Events and What Events Are Needed

1.) Since the user needs to click the submit button, you need to write a “click event procedure.” This procedure is simply a piece of VB.NET code.
2.) Double click the “Submit” button in the designer to access the event procedures.
3.) What you will see is this:

Public Class Form1

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

End Sub

End Class

Since you are beginner, you need to know the following basics in relation to events:

a.) VB.NET source code will show the default events of the control you are accessing. Since the control you have just clicked is a button control, the default event is “click”.

b.) The click event procedure is a block of code that you will insert between Private Sub Button1_Click and End Sub.

c.) Button1_Click is called the “event declaration”. It is formed by the name of the object (which is Button1) and the name of the event (which is click). They are separated by underscore.

d.) Also ByVal sender As System.Object and ByVal e As System.EventArgs are the parameters of the event. Actually, the name of the parameter is the text after ByVal. Sender is the parameter that is actually the source of the event .

In ByVal e As System.EventArgs , the parameter “e” is important in manipulating events of different objects. You will learn this in the coming tutorials relating to events. System.EventArgs are the data types.

4.) For now, you are only interested in writing a click event procedure. To start the procedure, you need to retrieve the name submitted by the text box:

Dim name As String = TextBox1.Text

And then assign this name to the Label2 control so that it will be shown back in the form after clicking the submit button.

Label2.Text = name
This is the complete code:

Public Class Form1

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

Dim name As String = TextBox1.Text
Label2.Text = name

End Sub

End Class

5.) Save the VB.NET project. Go back to the form in Designer view. Press F5 to run the project. Try entering a name and click Submit. Your name will then appear on the label below the button. See screenshot:

Accessing and Creating Different Events of Objects

So far you have learned how to access and write a click event procedure in your form application project.  However, you can access different events of different objects in your project. One of these objects is the form itself. Let’s have an example. Remember that before any user can type their name and click the submit button in the form, the label below the button (Label2) should always show the default text “Your name will show here”. How about removing this default text entirely during initial form loading and showing only the name of user after they click the submit button?

To do this, you need to use events associated with the form.

1.) Since you are interested in clearing the text of the label during the loading of the form, you will use the “load event” of the form object.

2.) To access this load event of the form, double click anywhere on the region of your form (but not on the text box, label or button control). VB.NET will show another procedure (in addition to the click event procedure created previously). This time it is called a “load event procedure” of the form. This is the actual procedure block:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Take note the event declaration “Form1_Load”, which means that this is the load event event procedure of Form1 control.

3.) It is now easy to set the label empty to blank/clear during initial form loading. You can do so with the code below:

Label2.Text = ""

This is the complete load event procedure to clear the text of the label during form loading:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Label2.Text = ""

End Sub

4.) Now save your project and go to designer view. Press F5 to run your project. You should not see the Label2 with the text “Your name will show here”. This is because it is set to empty during form loading.

5.) Try entering a name and then you will see Label2 with your name after clicking the submit button.

Object and Events Drop Down List Window

No one would like to memorize all of the events associated with objects in VB.NET. In fact, you do not need to because VB.NET includes an object and events drop down list window.

1.) Go to Form1.vb to see the source code of your project.
2.) Above the code, you will see two drop down list windows with the default values: “(General)” and “(Declarations)”.
3.) The first one on your left is the list of objects currently used by your project. Try expanding the drop down – you will see Form1 events, Button1, Label1, Label2, and Textbox1.

4.) Why is this so important? Suppose you need to create a “Textchanged event procedure” for your Textbox1 object; simply select “TextBox1” in the object window and then select “TextChanged” in the list of events associated with a textbox (under “Declarations”).

VB.NET will automatically add an event procedure to your code like this:

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

You can then write anything you want to accomplish when textchanged events are triggered.

[gp-comments width="770" linklove="off" ]