a.k.a. YOU decide when the application closes!
VB.NET 2002 and 2003
In the original versions of VB.NET if the Form used to start the application is closed then the application automatically exits. You know this, of course. You probably also know that if you want to include Splash screens. Log in forms or in fact any temporary form at the start of the application’s life, you have to jump through coding hoops to do this.
Of several variations of ways I’ve seen of cracking this one, I have always liked the AppMgr idea which Mike McIntyre covered in a series of DevCity Newsletter articles. Essentially you create a small Class that has a Sub Main plus form variables to reference those temporary forms (Splash, Login, etc).
Sub Main is set as the StartUp Object and the temporary forms are shown modally. When they close, the next form (if there is another one) will be created and displayed modally. Once the user has run through all the temporary screens, the first main form of the application is displayed.
Here’s the key part of the code from Mike’s article:
Friend Class AppMgr
_
Shared Sub Main()
' Provide Splash Form service.
Dim SplashFrm As New SplashForm()
SplashFrm.ShowDialog()
' Provide Login service.
Dim LoginFrm As New LoginForm()
LoginFrm.ShowDialog()
' Declare a variable named frm1 of type Form1.
Dim frm1 As Form1
' Instantiate (create) a new Form1 object and assign
' it to variable frm1.
frm1 = New Form1()
' Call the Application class' Run method
' passing it the Form1 object created above.
Application.Run(frm1)
End Sub
' Don't forget to set the StartUp Object as Sub Main and not
' the default setting of Form1!
End Class
Closing frm1 will still cause the application to end, but at least you will have managed to show the Splash and Login forms without problems.
VB.NET 2005 (VB2005)
One of the many small but really useful little tweaks that came along with VS2005 is the ability to decide whether closing the StartUp Form will or will not automatically close down the application. This can be done very simply in the Project’s Properties Window.
Here are the steps:
1. Right Click on the Project name.
2. Select “Properties”
3. In the Properties window, the default Tab Page named “Application” should be selected. If not, manually select it.
4. Towards the bottom of this page there is a ComboBox titled “Shutdown mode”. You can choose between :
a. When startup form closes
Or
b. When last form closes.
Makes life so much easier, doesn’t it?
Splash Screens
Oh, and speaking of Splash Screens …. VS2005 now ships with a ready made Splash Screen template you can use. Just select it when adding a new form to the project instead of going for the default Windows Form template (not entirely sure if this is available in all editions - any feedback on this appreciated).
And also, did you know that the Application Properties tab page also allows you to select a form as your initial splash screen?
With the new versatility in 2005, you can
a. Kick off the main application with Form1
b. Display a Splash Screen, and
c. Run the app until the point where all forms have been closed.
Now that, my friends, is progress !