Handling Form Events
The
base Form class may at times raise events. These events can be
handled by the derived Form class. One way to do this is to define a
handler subroutine that uses the MyBase keyword in
the Handles clause, like this:
' This is not the preferred technique.
Private Sub Form_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs _
) Handles MyBase.Closing
' ...
End SubHowever, a better technique is to override the protected methods, which are provided by the Form class for this purpose. For example, the following method could be placed in the derived class’s definition, providing a way to respond to the form’s imminent closing:
' Assumes Imports System.ComponentModel Protected Overrides Sub OnClosing( _ ByVal e As CancelEventArgs _ ) ' ... MyBase.OnClosing(e) ' Important End Sub
Note that the implementation of the OnClosing method includes a call to the base class’s implementation. This is important. If this is not done, the Closing event won’t be raised, which will affect the behavior of any other code that has registered for the event.
Following is the list of events the Form class defines, including a brief description of each event and the syntax for overriding the protected method that corresponds to each event. Note also that the Form class indirectly derives from the Control class and that the Control class also exposes events and overridable methods that aren’t shown here.
- Activated
Fired when the form is activated. Its syntax ...