skip to Main Content

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


  1. 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…

    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
    

    …do this…

    Private Sub timerSplash_Tick(sender As Object, e As EventArgs) Handles timerSplash.Tick
        timerSplash.Stop()
    
        Dim disclaimerForm As New Disclaimer()
        disclaimerForm.ShowDialog()
    
        Me.Close()
    End Sub
    

    If you want to not show the splash form, use Show instead of ShowDialog. Make sure to also set the shutdown mode to OnLastWindowClose as well.

    Login or Signup to reply.
  2. 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:

    Imports System.Threading
    Imports Timer = System.Timers.Timer
    
    Public Class SplashScreen
    
        Private Structure SplashScreenInfo
            Public StartupForm As Form
            Public MinimumDisplayTime As Integer
            Public Handle As ManualResetEvent
        End Structure
    
        Private timer As New Timer
        Private minimumDisplayTimeExpired As Boolean = False
        Private syncRoot As New Object
        Private closeHandle As ManualResetEvent
    
        Private Sub New(startupForm As Form, minimumDisplayTime As Integer)
            InitializeComponent()
    
            AddHandler startupForm.Load, AddressOf startupForm_Load
    
            AddHandler timer.Elapsed, AddressOf timer_Elapsed
            timer.Interval = minimumDisplayTime
            timer.Start()
        End Sub
    
        Public Shared Sub DisplaySplashScreen(startupForm As Form, minimumDisplayTime As Integer)
            Dim continueHandle As New ManualResetEvent(False)
    
            Call New Thread(AddressOf DisplaySplashScreen).Start(New SplashScreenInfo With
                                                                 {
                                                                     .StartupForm = startupForm,
                                                                     .MinimumDisplayTime = minimumDisplayTime,
                                                                     .Handle = continueHandle
                                                                 })
    
            continueHandle.WaitOne()
        End Sub
    
        Private Shared Sub DisplaySplashScreen(info As Object)
            Dim ssi = DirectCast(info, SplashScreenInfo)
            Dim SplashScreen As New SplashScreen(ssi.StartupForm, ssi.MinimumDisplayTime)
    
            ssi.Handle.Set()
    
            Application.Run(SplashScreen)
        End Sub
    
        Private Sub startupForm_Load(sender As Object, e As EventArgs)
            SyncLock syncRoot
                If (Not minimumDisplayTimeExpired) Then
                    closeHandle = New ManualResetEvent(False)
                End If
            End SyncLock
    
            If (closeHandle IsNot Nothing) Then
                closeHandle.WaitOne()
            End If
    
            Invoke(New MethodInvoker(AddressOf Close))
        End Sub
    
        Private Sub timer_Elapsed(sender As Object, e As Timers.ElapsedEventArgs)
            SyncLock syncRoot
                If closeHandle Is Nothing Then
                    minimumDisplayTimeExpired = True
                Else
                    closeHandle.Set()
                End If
            End SyncLock
        End Sub
    
    End Class
    

    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 own Load 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 the Timer in the splash screen form raises its Elapsed event. The time for that to happen is specified when you call DisplaySplashScree in the first place.

    Add a module named Program to your project and add the following code:

    Module Program
    
        <STAThread>
        Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
    
            Dim startupForm = New Form1
    
            SplashScreen.DisplaySplashScreen(startupForm, 3000)
    
            Application.Run(startupForm)
        End Sub
    
    End Module
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search