|
Start your application in Visual Studio 2003
By Simon Jorritsma
There are many ways for determining what the user will see when your
application starts. 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
From the Project menu:
- Right click on project
- Properties.
- General tab.
- In the Startup Object list box, select the form you want as the new startup form.
- Choose OK.
Sometimes you want to startup Form without any form initially loaded. For example, you might want to execute code that loads a
registry entry and then displays one of several different forms depending on what is in the
registry. You can do this by creating a Sub procedure called Main in a standard module, as in the following example:
Create a Sub main()
- Right click on project
- Add
- Add module
- Change name in StartUp.vb
- Type "Sub Main"
- Your code
- Right click on project
- Prperties
- General tab
- Startup Object choose Sub Main()
- Choose OK
Example sub Main()
|
Sub Main()
Dim strRegistryEntry As Integer
' Call a function procedure to check registry.
strRegistryEntry = GetUserStatus
' Show a startup form based on status.
If strRegistryEntry = 1 Then
frmMain.Show
Else
frmLanguage.Show
End If
|
This procedure must be a Sub procedure, and it cannot be in a form module.
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. 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()
appContext = New ApplicationContext()
fMainForm = New MainForm()
fSplash = New SplashForm()
'Show Splash Form
fSplash.Show()
'You can use a timer here to unload the splash
'screen after a certain amount of time or do other
'initialization tasks
'Show Main Form
fMainForm.Show()
appContext.MainForm = fMainForm
Application.Run(appContext)
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.
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
|
|