This documentation is archived and is not being maintained.

Overriding Event Handlers with Visual Basic .NET

Visual Studio .NET 2003
 

Matthew A. Stoecker
Visual Studio Team
Microsoft Corporation

February 2002

Summary: This paper shows how to override event handlers when programming in Visual Basic® .NET. How to use the Handles clause will also be discussed. (3 printed pages)

Contents

Introduction
Prerequisites
Inherited Event Handlers
Inherited Event Handlers in Components
Conclusion

Introduction

When you inherit from a control or a component, you create a new control or component that incorporates all of the functionality of its base class. Any event handlers that are defined by the base class will be included in the inherited component. This paper will examine the issues involved with inherited event handlers and discuss the ramifications for developing applications using Visual Basic .NET.

Prerequisites

  • You should have a good understanding of inheritance and object-oriented programming. For more information, see Polymorphism in Components

Inherited Event Handlers

When a component is inherited, all of the members of that component are incorporated into the new class. Event handlers are methods that are executed in response to specific events that the component receives, and are inherited along with the rest of the 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() & " time(s).")
End Sub

The above method is executed whenever the Button1.Click event is raised. The Handles clause at the end of the method declaration associates the method with the event. This structure is typical of the event handlers in components.

In order to allow this method to be overridden 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 overridden:

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() & " time(s).")
End Sub

Inherited Event Handlers in Components

Overriding an inherited event handler is the same as overriding any other kind of inherited method, with one important difference: When you override an inherited event handler, you have to remove the Handles clause.

To override a method in an inherited component

  • Add the Overrides keyword to your method declaration.
    Note   You should not add a Handles clause to the method. The event handler is already associated with the event in the base class, and this association is passed on to the inheriting class. In other words, the method will be executed when the event is fired and does not require an additional Handles clause.

The following example shows how to override the event handler from 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

How the Handles Clause Can Cause Problems in the Derived Class

The Handles clause is no longer associated with the method. This is not an oversight, but rather an important part of how events are handled in the .NET Framework. The event handler is already associated with the event in the base class, and this association is passed on to the inheriting class. In other words, the method will be executed when the event is fired and does not require an additional Handles clause. Adding an additional Handles clause, as shown below, will create an additional association with the event, and will cause the method to be executed twice per event.

' INCORRECT
Protected Overrides Sub Button1_Click(ByVal sender As System.Object, _
   ByVal e as System.EventArgs) Handles Button1.Click
   Static Counter as Integer = 0
   ' This variable will be incremented twice each time the button is 
   ' clicked.
   Counter += 1
   ' The message box will be displayed twice for every time the button is
   ' clicked, and will display inaccurate information.
   MessageBox.Show (" This inherited button has been clicked " & _
      Counter.ToString() & " times.")
End Sub

Conclusion

This problem caused by overriding event handlers might not be intuitive and can lead to bugs that are difficult to isolate. Setting the appropriate associations with your event handlers is very important. Use caution, and be aware of event associations that are already in place.

Show: