What s Controlling Our Form When We Run It?

What's Controlling Our Form When We Run It?

Before we leave the topic of forms, I'd like to cover one more important concept, that of the Application object. Hidden from view in our form is a procedure named Sub Main. This procedure is the real controller of our application.

In an application designed for the desktop, a controller that handles the application is required. In classic Visual Basic, we did have the global App object, but we didn't do much with it except grab the value of App.Name or some other property for display purposes. Still, the App object was responsible for running our application. Now, in Visual Basic .NET, we have to become friends with it.

When you run your project, Sub Main runs first. Sub Main calls the Application object's Run method and passes in as a parameter the form you want to display.

System.Windows.Forms.Application.Run(New Form1)

When this code runs, the New keyword causes Form1's constructor, Sub New, to be called, and a new form is created. In our example, the first form is automatically used as our startup form. The Application object uses this form to run. Let's check this out by taking a closer look.

Try This Out

Run your program again. Click the Clone Me! button four times to create four copies of the original form. Now close the original form, the one whose caption reads "Mirror Image". Phoof! All of the other forms disappear. When you dismiss the main form for the application, Form1 in our example, its destructor, Sub Dispose, is called. Because all copies of our class are based on this class and this class no longer exists, each of their destructors is called in turn.

The .NET Application object is a traffic cop. It is initialized with the form you want to show at startup. It is responsible for keeping the form object in scope and dispatching messages to the form or control that currently has the focus. Each form class has a shared Sub Main procedure that looks like the following:

Shared Sub Main()     System.Windows.Forms.Application.Run(New Form1) End Sub

If a form didn't use the Application.Run method, when you ran your program, the form would be displayed and then disappear instantaneously because it goes out of scope. In other words, the code executes in the correct order, but because nothing is there to keep it active, the form self-destructs.

The Application.Run method creates a message loop that keeps our form displayed and looks for messages from the operating system, such as mouse clicks or data entry, that are targeted at our program. Any messages for our form are routed to Application.Run. From there the messages are routed to the appropriate message handlers in the form, which may involve forwarding the message to contained objects such as our button or other contained controls. The loop will continue to run until the form is terminated.

If you want to change the startup form, you could add the code above and place the name of the form to run in the Run method of the Application object. Remember that because the Run method is shared, only a single copy runs for all forms.

Because the default behavior of an Application object is to have its Run method start the first form created, the designers of Visual Basic .NET elected to hide the method from us. But to get at it and use its power, simply create a new statement by typing application and a dot (.). IntelliSense will show you the Application object's properties and methods. You can see from Figure 2-16 that you can use the Application object to grab the product name or product version among other information. If you want to get and display the current product name or version, just interrogate (read) those properties.

Figure 2-16

Looking at the Application object's methods.

As in classic Visual Basic, the Application object runs our application in the background. In Visual Basic .NET, however, its use has been expanded and we can get at what we need. I'll cover more about the Application object as topics arise.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net