Home / Software / Declaring and Raising Events in Vbnet 2003

Declaring and Raising Events in Vbnet 2003

We can easily declare and raise an event in VB.Net 2003. As you know that event is like a signal to your application. It informs to the application that some action has been performed by the application user. In response to that action, there should be a method which handles and process the reaction of the triggered event. You can follow below steps to implement and handle any event in VB.Net 2003. Let’s look into those important steps.


You can define your event within classes, structure, modules and interfaces of VB.Net. You have to use the keyword “Event” while declaring any customized event. For example, consider following event declaration named as “TestEvent”.

Event TestEvent(ByVal TestValue As Integer)


You can raise any declared event using the word “RaiseEvent”. In VB.Net 2003, RaiseEvent keyword invokes the event which is specified along with this statement. For example, consider below example.

RaiseEvent TestEvent(TestValue)

One thing you must make sure that you cannot raise an event from derived class, if that event is declared in base class. You can raise the event within the scope of that declared event.

VB.Net controls viz. Forms, Command Buttons and other user defined objects can raise such events and hence they are termed as Event Sender. Technically event is initiated from them hence they are also called as Event Source.

Now we have to go for Event Handler. Event Handler is a subroutine or procedure which will be called when the event is raised or invoked. You have to associate the event with the event handler. This can be done by using Handles statement or AddHandler statement. AddHandler statement is more flexible as compared to Handles statement. The WithEvents statement and Handles clause will declare as wells as associate the event handler with the custom event.

Suppose we define a class called TestClassWithEvent

Public Class TestClassWithEvent

Public Event TestEvent(ByVal TestValue As Integer)

Public Sub TestClassProcedure()

‘Your logic goes here

RaiseEvent TestEvent(TestValue)

End Sub

End Class

Our class TestClassWithEvent declares the event called TestEvent and sample method called TestClassProcedure. TestClassProcedure will raise the event TestEvent.

Now we need to create the object of this class and use it in our project Form or other class module. Suppose we can create response to the event using the class TestClassWithEvent as given below.

Public WithEvents TestObject As TestClassWithEvent

Set TestObject = New TestClassWithEvent

Here we must make sure that declaration and instantiation of object should be done separately. We can’t do both the things i.e. declaration and instantiation of the class (using WithEvents) simultaneously.

Private Sub TestObject_TestEvent(ByVal sender As System.Object,_

ByVal e As System.EventArgs) Handles TestObject.TestEvent

‘Your logic goes here

End Sub

The event handler arguments, “sender” and “e”, are the same for all event subroutines, in part, so you can code an event subroutine that handles different types of events like the example above. But these arguments don’t carry the same information all of the time, even though their names and types are the same. The “sender” argument provides information about the event that was raised. This method of associating an event with the event handler is called “early binding” because VB.Net 2003 already knows the type of the object when the code is compiled.

Instead of using Handles clause and WithEvents statement, you can use AddHandler statement which dynamically associates an event with the event handler and the method is called “late binding”. In the method we define the object of the class TestClassWithEvent in a normal manner.

Public TestObject As New TestClassWithEvent

Now add the event handler by using following code.

Protected Sub TestObject_TestEvent(ByVal TestValue As Integer)

‘Your logic goes here

End Sub

Now we can associate the event TestEvent with the above event handler with AddHandler statement. AddHandler takes two arguments: the name of an event from an event sender such as a control, and an expression that evaluates to a delegate. Delegates are objects you can use to call the methods of other objects. You do not need to explicitly specify the delegate class when using AddHandler, since the AddressOf statement always returns a reference to the delegate. The following example associates an event handler with an event raised by an object:

AddHandler TestObject.TestEvent, AddressOf Me.TestObject_TestEvent

About User Lin