GUI applications are event-driven, that is, the application executes code in response to user events, such as clicking the mouse, choosing a menu item, and so on. Each form or control has a predefined set of events. For example, every form has a MouseDown event and every control has a Click event. Windows Forms employs the .NET event model, [1] which uses delegates to bind events to the methods that handle them. The Windows Forms classes use multicast delegates. A multicast delegate maintains a list of the methods it is bound to. When an event occurs in an application, the control raises the event by calling the delegate for that event. The delegate then calls all the methods it is bound to. However, VB.NET hides most of this from the programmer. [1] You may wish to review the discussion of delegates and events in Chapter 6. To handle the Click event of a Button object, the following code would be required: Dim WithEvents btnCalc As Button = New Button() ... Public Sub btnCalc_Click(ByVal sender As System.Object, _ ByVal e as System.EventArgs) Handles btnCalc.Click End Sub The event handler uses the Handles keyword to indicate what event from which object that it responds to. The handler for the Click event receives parameters representing the sending object and corresponding event arguments. To design a handler procedure that can handle events for more than one control, you must identify each event using Handles . Then, you must use the sender parameter to determine which control generated the event. Dim WithEvents btnCalc As Button = New Button() Dim WithEvents btnClear As Button = New Button() ... ' In the constructor, you would assign each control a name btnCalc.Name = "btnCalc" btnClear.Name = "btnClear" ... Public Sub ClearOrCalc(ByVal sender As System.Object, _ ByVal e as System.EventArgs) Handles btnClear.Click, _ btnCalc.Click If CType(sender, Button).Name = "btnClear" Then ' logic for btnClear ElseIf CType(sender, Button).Name = "btnCalc" Then ' logic for btnCalc End If End Sub You can find all the events associated with a class in the .NET Framework documentation. The screen shot in Figure 7-7 shows the predefined events associated with the Control class. Figure 7-7. Documentation of events in the Control class. Step 3: Adding a Button to the Form In Step 3 of our ByHand example, we will add a button to the form. It's caption will be "Click Me." When clicked, it will display a message box containing the greeting "Hello World." Figure 7-8 shows a run of the application. Figure 7-8. Using a button and displaying a message box with a form (Step 3). Here is the code for Step 3. Imports System.Windows.Forms Class MainWindow Inherits System.Windows.Forms.Form Private WithEvents btnClickMe As Button Public Sub New() MyBase.New() Me.Text = "First Program" Me.Height = 175 InitializeComponent() End Sub Private Sub InitializeComponent() btnClickMe = New Button() btnClickMe.Name = "btnClickMe" btnClickMe.Text = "Click Me" btnClickMe.Size = _ New System.Drawing.Size(Me.ClientSize.Width/2, _ Me.ClientSize.Height/2) btnClickMe.Location = _ New System.Drawing.Point(Me.ClientSize.Width/4, _ Me.ClientSize.Height/4) Me.Controls.Add(btnClickMe) End Sub Public Sub btnClickMe_Click(ByVal sender As _ System.Object, ByVal e as System.EventArgs) _ Handles btnClickMe.Click MessageBox.Show("Hello World", "Greeting") End Sub Public Shared Sub Main() Application.Run(new MainWindow()) End Sub End Class The class contains the variable btnClickMe that represents the instance of the button. It was declared using WithEvents so that we could handle the events that it generates. The constructor has been designed to call InitializeComponent to instantiate the button, then set the Name and Text properties. Our algorithm for the button Size and Location properties indicates that the button consumes half of the width and half of the height of the client area of the form, and is centered within the form. The Size and Point classes, which are used for the button's Size and Location properties, are in the System.Drawing namespace. The button is then added to the form's Controls collection. Finally, the btnClickMe_Click procedure was defined to handle the Click event of btnClickMe . It uses the MessageBox class to display a simple message box with a caption of "Greeting" and containing the message "Hello World." You can build the application at the command line using the batch file build.bat . We have had to add an additional reference. System.Drawing.dll is required because the Point and Size types we referenced are defined in this library. Our build.bat file now resembles: vbc /t:winexe /r:System.dll,System.Drawing.dll, System.Windows.Forms.dll BasicWindow.vb Step 4: Using Label and TextBox Controls Step 4 of our ByHand application illustrates the use of a Label and a TextBox control to collect the user's name. The name is then used in the greeting that is displayed when the "Click Me" button is clicked. The TextBox control allows you to insert characters wherever you wish in the control, cut and paste (Ctrl+X and Ctrl+V), and so forth. Figure 7-9 illustrates the application after a name has been entered and the "Click Me" button clicked. Figure 7-9. Using a label and text box on a form (Step 4). Here is the final version of Step 4. We had to run it many times with different values for the Location and Size properties of each control until we found values that made our form look the way we wanted it to. Imports System.Windows.Forms Class MainWindow Inherits System.Windows.Forms.Form Private WithEvents lblName As Label Private WithEvents txtName As TextBox Private WithEvents btnClickMe As Button Public Sub New() MyBase.New() Me.Text = "First Program" Me.Height = 175 InitializeComponent() End Sub Private Sub InitializeComponent() lblName = New Label() lblName.Name = "lblName" lblName.Text = "Your Name:" lblName.Size = New System.Drawing.Size(75, 30) lblName.Location = New System.Drawing.Point(15,25) Me.Controls.Add(lblName) txtName = New TextBox() txtName.Name = "txtName" txtName.Text = "" txtName.Size = New System.Drawing.Size(175, 30) txtName.Location = New System.Drawing.Point(100,25) Me.Controls.Add(txtName) btnClickMe = New Button() btnClickMe.Name = "btnClickMe" btnClickMe.Text = "Click Me" btnClickMe.Size = _ New System.Drawing.Size(260, 60) btnClickMe.Location = _ New System.Drawing.Point(15, 75) Me.Controls.Add(btnClickMe) End Sub Public Sub btnClickMe_Click(ByVal sender As _ System.Object, ByVal e as System.EventArgs) _ Handles btnClickMe.Click MessageBox.Show("Hello " & txtName.Text, "Greeting") End Sub Public Shared Sub Main() Application.Run(new MainWindow()) End Sub End Class As you can see, using the Label and TextBox controls is very easy. We instantiate them and assign initial values to their Location , Size , and Text properties. We can reference the text displayed by the label or entered by the user in the text box by referencing the control's Text property. |