1. Technology

When Form.Load isn't enough

Writing your own Main routine


Updated January 04, 2007

Most Windows applications use the Form.Load event subroutine for initialization code, such as setting the starting value of variables or perhaps downloading some initial information from a database. But what if your program needs more control? You might want to put all of your intialization code in a different module for security reasons, for example. How do you get your own code to execute first in a standard Windows applcation? The basic strategy is to code your own Sub Main in a new module. After this, you can choose to turn control over to a startup form ... or not. At this point, you're in complete control.

Visual Basic 2005 has changed a lot from the way it was done in VB 2003. Most of the change can be seen in the difference in the Project Properties dialog. In VB 2003, a hierarchical display allowed you to select different dialog windows. In VB 2005, this has become a tabbed dialog display in the main edit window. VB 2005 Express and VS 2005 Pro are largely identical, by the way, with the major difference being that the "Pro" version gives you control over project configuration (debug, release, or your own unique configuration). It's just another example of how Microsoft is really giving away a lot with their free "Express" versions.

This article assumes that you're running VB 2005.

Click Here to display the illustration
Click the Back button on your browser to return

To create your own Sub Main, first add a new module to the project. In the module, add this code:

Module Module1
   Public Sub Main()
   End Sub
End Module

Now open the Project Properties dialog for the application. See the illustration linked above for details. Uncheck the Enable Application Framework checkbox. This turns off all of the options in the frame below it since those are the things that the Application Framework does. These are new in VB 2005, too. Notice that when you uncheck the box, the option above it changes from Startup Form to Startup Object. When you're in control, you can choose anything you like to start your application, not just a form. So finally, select the Sub Main from the Startup Object dropdown.

If you don't add any other code, then the application will run pretty much as it would if you had just left Form1 as the Startup Form. But you now have a module that gets control before anything else happens. Check it yourself and see. Add a statement ...

MsgBox("Before Form1 Runs")

... in front of the Run statement.

You might wonder why you have to code Application.Run(Form1) instead of simply displaying the form with Form1.Show. Try it and see. If you make this change, you'll see that Form1 does display very briefly on the screen and then the application shuts down again.

The reason this happens is that the Run statement also starts what is called the Windows message pump. This is a routine that is internal to Windows classes that continually checks the Windows message queue. This queue is at the heart of Windows and it tells all software running what is happening. The queue has entries for every mouse movement, clock pulse, system interrupt ... even events created by your program such as when the font or color is changed. In other words, everything is added to the message queue and applications check it to see if they should react. In Windows, the message pump is known as the WndProc procedure. Every form and every control has one and they all check the message queue. This is what makes Windows event coding work.

But the Form1.Show statement shows the form ... and that's all. So since the application doesn't have any other work, it shuts down.

This isn't the only way to let your program get control in VB 2005, however. Microsoft has added some new events to VB 2005 that are called Application Events. You can use them to add code that runs before Form1 loads too. These events are enabled only when the Enable Application Framework checkbox is checked. Click View Application Events in the Application tab of the project properties and you can then select MyApplication Events from the Class Name dropdown and pick from ...

  • Startup
  • Shutdown
  • UnhandledException
  • StartupNextInstance
  • NetworkAvailabilityChanged

... in the Method Name dropdown. Using the Startup event is similar to what we have already done.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. When Form.Load isn't enough

©2014 About.com. All rights reserved.