Visual Studio 2022. VB.NET. Windows Form. Two forms. Splash & Disclaimer. Splash form should stay on screen for 3 seconds, unload and load Disclaimer from. Splash form is working great, but Disclaimer for does not stay on screen for next action. For now the disclaimer form is empty. I have added a timer control in the splash form and code is below. Disclaimer form stays on screen if I make it the startup form and run the project.
Private Sub timerSplash_Tick(sender As Object, e As EventArgs) Handles timerSplash.Tick
timerSplash.Stop()
Me.Close()
Dim disclaimerForm As New Disclaimer()
disclaimerForm.ShowDialog()
End Sub
2
Answers
You need to close the splash screen after you open the new dialog, not before it. Otherwise, if this is the only form, the application will close.
Instead of…
…do this…
If you want to not show the splash form, use
Show
instead ofShowDialog
. Make sure to also set the shutdown mode toOnLastWindowClose
as well.This is not an answer to the question per se but, rather, is an alternative way to display a splash screen. It’s a better way than what’s being used here.
I originally implemented this idea some years ago, to provide splash screen functionality for C# applications that behaved somewhat like the splash screen functionality built into VB apps via the Application Framework. Go here to see the original C# code and read some explanation of the principles involved.
Here is a VB implementation of that code. Add a new form to your code named
SplashScreen
and add the following code:That code handles the
Load
event of the startup form and prevents the event handler completing until a wait handle is triggered. Note that this event handler will always be executed after the forms ownLoad
event handler, if there is one, thus any work done to initialise the form in that event handler will be done before waiting. The wait handle is triggered when theTimer
in the splash screen form raises itsElapsed
event. The time for that to happen is specified when you callDisplaySplashScree
in the first place.Add a module named
Program
to your project and add the following code:This assumes that you have a
Form1
form in your project that you want to be the main/startup form. It creates the startup form, then displays the splash screen, then starts the app with the specified startup form. As mentioned above, that startup form will be initialised but won’t be displayed until the splash screen closes, which happens after 3000 milliseconds in this case.You will need to disable the Application Framework in the project properties and set the startup object to
Sub Main
to make that module the entry point for the app.I forgot to mention one of the main premises of this code. The splash screen is created and displayed on a different thread to the startup form, just as the VB Application Framework does. The point of that is that it actually enables a splash screen to do what they were originally intended for: to give the user something to look at while the application is initialised. If you display the splash screen by calling
ShowDialog
on the UI thread then that will block and prevent you doing any further work in the startup form. That’s fine if your startup form doesn’t really do anything and the splash screen is just a billboard. If there is actually initialisation taking place in the startup form though, you need the splash screen to be created on a different thread, so the UI thread is free to do that work.