Share via


Visual Basic Concepts

More About Forms

In addition to the basics of form design, you need to think about the beginning and end of your application. There are several techniques available for determining what the user will see when your application starts. It's also important to be aware of the processes that occur when an application is unloaded.

Setting the Startup Form

By default, the first form in your application is designated as the startup form. When your application starts running, this form is displayed (so the first code to execute is the code in the Form_Initialize event for that form). If you want a different form to display when your application starts, you must change the startup form.

To change the startup form

  1. From the Project menu, choose Project Properties.

  2. Choose the General tab.

  3. In the Startup Object list box, select the form you want as the new startup form.

  4. Choose OK.

Starting Without a Startup Form

Sometimes you might want your application to start without any form initially loaded. For example, you might want to execute code that loads a data file and then displays one of several different forms depending on what is in the data file. You can do this by creating a Sub procedure called Main in a standard module, as in the following example:

Sub Main()
   Dim intStatus As Integer
   ' Call a function procedure to check user status.
   intStatus = GetUserStatus
   ' Show a startup form based on status.
   If intStatus = 1 Then
      frmMain.Show
   Else
      frmPassword.Show
   End If

This procedure must be a Sub procedure, and it cannot be in a form module. To set the Sub Main procedure as the startup object, from the Project menu, choose Project Properties, select the General tab, and select Sub Main from the Startup Object box.

Displaying a Splash Screen on Startup

If you need to execute a lengthy procedure on startup, such as loading a large amount of data from a database or loading several large bitmaps, you might want to display a splash screen on startup. A splash screen is a form, usually displaying information such as the name of the application, copyright information, and a simple bitmap. The screen that is displayed when you start Visual Basic is a splash screen.

To display a splash screen, use a Sub Main procedure as your startup object and use the Show method to display the form:

Private Sub Main()
   ' Show the splash screen.
   frmSplash.Show
   ' Add your startup procedures here.
   …
   ' Show the main form and unload the splash screen.
   frmMain.Show
   Unload frmSplash
End Sub

The splash screen occupies the user's attention while your startup procedures are executing, giving the illusion that the application is loading faster. When the startup procedures are completed, you can load your first form and unload the splash screen.

In designing a splash screen, it's a good idea to keep it simple. If you use large bitmaps or a lot of controls, the splash screen itself may be slow to load.

Ending an Application

An event-driven application stops running when all its forms are closed and no code is executing. If a hidden form still exists when the last visible form is closed, your application will appear to have ended (because no forms are visible), but will in fact continue to run until all the hidden forms are closed. This situation can arise because any access to an unloaded form's properties or controls implicitly loads that form without displaying it.

The best way to avoid this problem when closing your application is to make sure all your forms are unloaded. If you have more than one form, you can use the Forms collection and the Unload statement. For example, on your main form you could have a command button named cmdQuit that lets a user exit the program. If your application has only one form, the Click event procedure could be as simple as this:

Private Sub cmdQuit_Click ()
   Unload Me
End Sub

If your application uses multiple forms, you can unload the forms by putting code in the Unload event procedure of your main form. You can use the Forms collection to make sure you find and close all your forms. The following code uses the forms collection to unload all forms:

Private Sub Form_Unload (Cancel As Integer)
   Dim i as integer
   ' Loop through the forms collection and unload
   ' each form.
   For i = Forms.Count – 1 to 0 Step - 1
      Unload Forms(i)
   Next
End Sub

There may be cases where you need to end your application without regard for the state of any existing forms or objects. Visual Basic provides the End statement for this purpose.

The End statement ends an application immediately: no code after the End statement is executed, and no further events occur. In particular, Visual Basic will not execute the QueryUnload, Unload or Terminate event procedures for any forms. Object references will be freed, but if you have defined your own classes, Visual Basic will not execute the Terminate events of objects created from your classes.

In addition to the End statement, the Stop statement halts an application. However, you should use the Stop statement only while debugging, because it does not free references to objects.

**For More Information   **For information on the Stop statement, see "Using Break Mode" in "Error Handling and Debugging," and "Stop Statement" in the Language Reference. For information on the forms collection or freeing references to objects, see "Programming with Objects."