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 is:
Protected Overrides Sub OnActivated(ByVal e As System.EventArgs)
- Closed
Fired when the form has been closed. Its syntax is:
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
- Closing
Fired when the form is about to close. Its syntax is:
Protected Overrides Sub OnClosing( _ ByVal e As System.ComponentModel.CancelEventArgs)
The CancelEventArgs.Cancel property can be set to
True
to prevent the form from closing; its default value isFalse
.- Deactivate
Fired when the form is deactivated. Its syntax is:
Protected Overrides Sub OnDeactivate(ByVal e As System.EventArgs)
- InputLanguageChanged
Fired when the form’s input language has been changed. Its syntax is:
Protected Overrides Sub OnInputLanguageChanged( _ ByVal e As System.Windows.Forms.InputLanguageChangedEventArgs)
The InputLanguageChangedEventArgs class has three properties that identify the new language: CharSet, which defines the character set associated with the new input language; Culture, which contains the culture code (see Appendix C) of the new input language; and InputLanguage, which contains a value indicating the new language.
- InputLanguageChanging
Fired when the form’s input language is about to be changed. Its syntax is:
Protected Overrides Sub OnInputLanguageChanging( _ ByVal e As System.Windows.Forms.InputLanguageChangingEventArgs)
The InputLanguageChangingEventArgs class has a Culture property that identifies the proposed new language and locale. It also has a Cancel property that can be set to
True
within the event handler to cancel the change of input language; the default value of the Cancel property isFalse
.- Load
Fired when the form is loaded. Its syntax is:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
- MaximizedBoundsChanged
Fired when the value of the form’s MaximizedBounds property (which determines the size of the maximized form) is changed. Its syntax is:
Protected Overrides Sub OnMaximizedBoundsChanged( _ ByVal e As System.EventArgs)
- MaximumSizeChanged
Fired when the value of the form’s MaximumSize property (which defines the maximum size to which the form can be resized) is changed. Its syntax is:
Protected Overrides Sub OnMaximumSizeChanged(ByVal e As System.EventArgs)
- MdiChildActivate
Fired when an MDI child window is activated. Its syntax is:
Protected Overrides Sub OnMdiChildActivate(ByVal e As System.EventArgs)
- MenuComplete
Fired when menu selection is finished. Its syntax is:
Protected Overrides Sub OnMenuComplete(ByVal e As System.EventArgs)
- MenuStart
Fired when a menu is displayed. Its syntax is:
Protected Overrides Sub OnMenuStart(ByVal e As System.EventArgs)
- MinimumSizeChanged
Fired when the value of the form’s MinimumSize property (which defines the minimum size to which the form can be resized) is changed. Its syntax is:
Protected Overrides Sub OnMinimumSizeChanged(ByVal e As System.EventArgs)
Get Programming Visual Basic .NET now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.