visual| Program
Using Visual Basic. NET Overload Event handlers
Matthew A. Stoecker
Visual Studio Team
Microsoft Corporation
February 2002
Summary: This article describes how to overload an event handler when you are programming in Visual Basic. NET. It also discusses how to use the Handles clause.
Directory
Brief introduction
Premise
An inherited event handler
An inherited event handler in an assembly
Summarize
Brief introduction
When you inherit a control or component, you create a new control or new component that encapsulates all the functionality of the base class. All event handlers defined by the base class are included in the inherited component. This article analyzes issues related to inherited event handlers and discusses how to use Visual Basic. NET to develop applications.
Premise
You should be very knowledgeable about inheritance and object-oriented programming. For more information, see Polymorphism in Components (English).
An inherited event handler
When you inherit a component, all members of that component are merged into the new class. An event handler is a method that is executed when responding to a specific event that a component receives, and is inherited with other component members. The following example shows a typical event handler:
Private Sub button1_click (ByVal sender as System.Object, ByVal E As _
System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This button has been clicked" & _
Counter.tostring () & "times. ")
End Sub
The above method is executed whenever the Button1.Click event occurs. The Handles clause at the end of the method declaration associates the method with the event. This is the typical structure of the event handlers in the component.
In order to overload this method in an inheriting class, you must add the Overridable keyword and change the access level to Protected, Protected Friend, or public. The following example shows an event handler that can be overloaded:
Protected Overridable Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This button has been clicked" & _
Counter.tostring () & "times. ")
End Sub
An inherited event handler in an assembly
Overloaded event handlers are essentially the same as overloading any other type of inheritance method, except that the Handles clause must be deleted when overloading an inherited event handler.
Overloading a method in an inheritance component
Add the Overrides keyword to the method declaration.
Note: Do not add a Handles clause to the method. The event handler is already associated with an event in the base class, and this association is passed into the inheritance class. That is, this method is executed when an event is raised, and no additional Handles clause is required.
The following example shows how to overload the event handlers in the previous example:
Protected Overrides Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs)
Static Counter as Integer = 0
Counter + 1
MessageBox.Show ("This inherited button has been clicked" & _
Counter.tostring () & "times. ")
End Sub
Why don't I need a Handles clause
The Handles clause is no longer associated with this method. This is not an oversight, but an important part of the. NET framework for handling events. The event handler is already associated with an event in the base class, and this association is passed into the inheritance class. That is, this method is executed when an event is raised, and no additional Handles clause is required. As shown below, if you add a Handles clause, an additional association with the event is created, which causes the method to be executed two times per event.
' Incorrect code
Protected Overrides Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
Static Counter as Integer = 0
' Each time the button is clicked, this variable increments
' two times.
Counter + 1
' Each time the button is clicked, the message box will display
' two times, and displays inaccurate information.
MessageBox.Show ("This inherited button has been clicked" & _
Counter.tostring () & "times. ")
End Sub
Summarize
Overloaded event handlers can cause some problems that are not easy to detect and result in difficult to detect errors. Therefore, it is important to set up the correct association with an event handler. Please use caution and note the event associations that already exist.