Handling Form Events Windows Forms I: Developing Desktop Applications

143 With lblHelloWindows .Font = New FontArial, 24 .Location = New Point16, 8 .Size = New Size248, 40 .TabIndex = 0 .Text = Hello, Windows End With Instantiate a button control and set its properties. btnOK = New Button With btnOK .Location = New Point320, 16 .TabIndex = 1 .Text = OK End With Set properties on the form. FormBorderStyle = FormBorderStyle.FixedToolWindow ClientSize = New Size405, 61 Text = Programming Visual Basic .NET Add the controls to the forms Controls collection. Controls.AddlblHelloWindows Controls.AddbtnOK End Sub System.STAThreadAttribute Public Shared Sub Main System.Threading.Thread.CurrentThread.ApartmentState = _ System.Threading.ApartmentState.STA Application.RunNew HelloWindows End Sub Main End Class

4.2 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 classs definition, providing a way to respond to the forms imminent closing: Assumes Imports System.ComponentModel Protected Overrides Sub OnClosing _ ByVal e As CancelEventArgs _ ... MyBase.OnClosinge Important End Sub Note that the implementation of the OnClosing method includes a call to the base classs implementation. This is important. If this is not done, the Closing event wont be raised, which will affect the behavior of any other code that has registered for the event. 144 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 arent shown here. Activated Fired when the form is activated. Its syntax is: Protected Overrides Sub OnActivatedByVal e As System.EventArgs Closed Fired when the form has been closed. Its syntax is: Protected Overrides Sub OnClosedByVal 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 is False . Deactivate Fired when the form is deactivated. Its syntax is: Protected Overrides Sub OnDeactivateByVal e As System.EventArgs InputLanguageChanged Fired when the forms 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 forms 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 is False . Load 145 Fired when the form is loaded. Its syntax is: Protected Overrides Sub OnLoadByVal e As System.EventArgs MaximizedBoundsChanged Fired when the value of the forms 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 forms MaximumSize property which defines the maximum size to which the form can be resized is changed. Its syntax is: Protected Overrides Sub OnMaximumSizeChangedByVal e As System.EventArgs MdiChildActivate Fired when an MDI child window is activated. Its syntax is: Protected Overrides Sub OnMdiChildActivateByVal e As System.EventArgs MenuComplete Fired when menu selection is finished. Its syntax is: Protected Overrides Sub OnMenuCompleteByVal e As System.EventArgs MenuStart Fired when a menu is displayed. Its syntax is: Protected Overrides Sub OnMenuStartByVal e As System.EventArgs MinimumSizeChanged Fired when the value of the forms MinimumSize property which defines the minimum size to which the form can be resized is changed. Its syntax is: Protected Overrides Sub OnMinimumSizeChangedByVal e As System.EventArgs

4.3 Relationships Between Forms