|
Our simplest application demonstrated a few necessary features, but other than that it’s extremely boring. Our form doesn’t even have an OK button. I will now demonstrate a less trivial example, a screen shot of which is shown in Figure 5-2. I’ve switched to Visual Studio for this example, even though I didn’t have to, because its editor makes manipulating the controls much easier.
A more complex Windows Forms example starts here.
Figure 5-2: A more complex form created with Visual Studio.
You will find that the Windows Forms programming model closely resembles the Visual Basic 6.0 programming model, although it’s available to any common language runtime language. That’s a good idea because Visual Basic’s programming model was and is immensely popular. It’s the language behind it that many programmers couldn’t stand, myself included. Now we can use any runtime-compliant language that we want, in addition to which Visual Basic has gotten much smarter, so I like it better. Besides, the C++ guys won most of the arm wrestling over the object programming model discussed in Chapter 2. An optimist would say that Microsoft was blending the best features of two design philosophies. A pessimist would say that Visual Basic programmers needed a consolation prize.
The Windows Forms programming model resembles the familiar Visual Basic 6.0 programming model.
Forms usually contain controls to obtain input from and display output to the user. The Windows Forms package contains a rich set of controls, just as Visual Basic 6.0 does. In writing this sample program, I dragged controls from the Visual Studio toolbox onto my form, as shown in Figure 5-3. When I did this, Visual Studio generated code in my form that creates the controls, sets their properties, and places them at the proper position when the form is first created. An excerpted version of this method is shown in Listing 5-2.
Controls on a form are created by explicit function calls generated by Visual Studio.
Figure 5-3: The Visual Studio toolbox.
Listing 5-2: Excerpted version of the InitializeComponent method showing control creation.
‘NOTE: The following procedure is required by the Windows Form Designer. ‘It can be modified using the Windows Form Designer. ‘Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Private Sub InitializeComponent() ’ Create controls. Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() ’ Set button properties. Me.Button1.Location = New System.Drawing.Point(88, 104) Me.Button1.Size = New System.Drawing.Size(112, 40) Me.Button1.TabIndex = 1 Me.Button1.Text = "Then Click Here" ’ Set text box properties. Me.TextBox1.Location = New System.Drawing.Point(40, 32) Me.TextBox1.Text = "Enter Text Here" Me.TextBox1.TabIndex = 0 Me.TextBox1.Size = New System.Drawing.Size(224, 20) ’ Set form properties. Me.Text = "Not Quite As Simple Windows Form VB" Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.TextBox1, Me.Button1}) End Sub
For the editor’s logistical convenience, this code lives in a private method called InitializeComponent, which is called from the form’s constructor. In Visual Basic .NET, the controls on a form are created by code that you can actually see, instead of by some invisible hand behind the scenes, as they were in Visual Basic 6.0. The newly created controls are added to a collection called Controls, which our form inherited from its Control base class. This collection allows a form to keep track of all of its controls.
The controls fire events in response to their interaction with the human user. For example, a button control fires an event signaling a click. We need to write event handler functions that will be called when the control signals an event. In Visual Basic, we can do this by adding a method called <Control_name>_<Event_name> to the form class, in this case, Button1_Click, shown in Listing 5-3. Visual Studio does this for us when we double-click on the button control in the editor, but anyone writing code in Notepad could simply add the method to the form class’s code. The Visual Basic compiler automatically injects code to set up an event handler on demand. In C#, we need to do a little more work, as described in Chapter 8, but that’s also done for us when we use Visual Studio. When the control fires an event, the CLR’s event mechanism looks for a handler function connected to the event and calls it if it finds one. You can also add an event handler dynamically as your code executes via the Visual Basic function AddHandler, not described here. I describe event handlers and the event process in detail in Chapter 8.
Controls fire events to handlers in your code.
Listing 5-3: Event handler for a button click.
‘ User clicked the button. Display a message box containing the ‘ text currently in the text box. Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click MessageBox.Show("You entered: " + TextBox1.Text) End Sub
Forms support deterministic finalization, which I discussed in the section about garbage collection in Chapter 2. The base class System.Windows. Forms.Form contains a method called Dispose that allows a client to immediately liquidate a form and free its resources without having to wait for or cause a complete garbage collection. This is especially important in Windows Forms because every form contains an operating system window handle for each control it contains, plus one for the form itself. These window handles are somewhat scarce in Windows 98 (don’t get me started), and deterministic finalization allows a client to release these resources as soon as it has finished with a form. You will see when you examine the source code that Visual Studio automatically overrides this method, adding code that calls the Dispose method of all the controls that the form contains.
A form supports the Dispose method for deterministic finalization.
Most of the events fired by Windows Forms controls originate low- level operating system notifications known as windows messages. As I mentioned previously, reading an early edition of Petzold explains what these messages are and where they come from. You can examine the messages going to your Windows Forms apps with the utility program SPY++.
|