Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

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 Sub

However, 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata