Adding event handlers Creating a Form in Code

142 .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 An Imports statement was added to give access to types in the System.Drawing namespace, such as Point and Size.

4.1.2.1 Adding event handlers

Define event handlers directly in code for any events that you wish to handle. For example: Private Sub btnOK_ClickByVal sender As Object, _ ByVal e As System.EventArgs Handles btnOK.Click Close Dispose End Sub The complete code for a standalone Windows Forms application is shown in Ex am ple 4- 3 . Compile it from the command line with this command: vbc HelloWindows.vb r:System.dll,System.Drawing.dll,System.Windows.Forms.dll t:winexe Note that the command should be typed on a single line. Example 4-3. Hello, Windows code generated outside of Visual Studio Imports System.Drawing Imports System.Windows.Forms Public Class HelloWindows Inherits Form Private lblHelloWindows As Label Private WithEvents btnOK As Button Private Sub btnOK_ClickByVal sender As Object, _ ByVal e As System.EventArgs Handles btnOK.Click Close Dispose End Sub Public Sub New Instantiate a label control and set its properties. lblHelloWindows = New Label 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.