Results 1 to 8 of 8

Thread: Messagebox in form load event hangs splash screen

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Messagebox in form load event hangs splash screen

    I have a splash screen in my application that pops up & goes away as expected when it starts up. When I add a messagebox in the form load event, the splash screen pops up & hangs the app. I'm guessing the messagebox is popping up behind the splash screen & is waiting for input. Can anyone tell me why this is happening? Why doesn't the splash screen still go away, even with the messagebox? What I want to happen is for the splash screen to pop up, go away after a few seconds & show the messagebox. The user then clicks the OK button on the messagebox & then the application form displays. Is there a way to make it work like this? Thanks...

  2. #2
    Member
    Join Date
    Sep 2024
    Posts
    49

    Re: Messagebox in form load event hangs splash screen

    Code:
    Public Class splash
        Inherits Form
        Public Sub New()
            Me.Text = "Splash Screen"
            Me.Size = New Size(300, 200)
            Me.StartPosition = FormStartPosition.CenterScreen
            Me.Show()
            Dim timer As New Timer()
            AddHandler timer.Tick, AddressOf OnTimerTick
            timer.Interval = 3000 ' 3 seconds
            timer.Start()
        End Sub
    
        Private Sub OnTimerTick(sender As Object, e As EventArgs)
            Dim timer As Timer = CType(sender, Timer)
            timer.Stop()
            timer.Dispose()
            Me.Hide()
            MessageBox.Show("Welcome to the Application!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Dim mainForm As New MainForm()
            mainForm.Show()
        End Sub
    End Class
    Public Class MainForm
        Inherits Form
        Public Sub New()
            Me.Text = "Main Application"
            Me.Size = New Size(800, 600)
        End Sub
    End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Messagebox in form load event hangs splash screen

    Quote Originally Posted by nbrege View Post
    I have a splash screen in my application that pops up & goes away as expected when it starts up. When I add a messagebox in the form load event, the splash screen pops up & hangs the app. I'm guessing the messagebox is popping up behind the splash screen & is waiting for input. Can anyone tell me why this is happening? Why doesn't the splash screen still go away, even with the messagebox? What I want to happen is for the splash screen to pop up, go away after a few seconds & show the messagebox. The user then clicks the OK button on the messagebox & then the application form displays. Is there a way to make it work like this? Thanks...
    Your problem is because MessageBoxes are modal. As soon as you show the MessageBox, all of your other code is stopped waiting for the MessageBox to close. There are 2 possible fixes. 1st is to show the MessageBox in a secondary thread, or 2nd, something similar to Red.x’s Timer idea…

    Edit: Possibly, there’s an overload of MessageBox.Show that allows you to choose a parent or an owner, which would place it in front of the specified form…
    Last edited by .paul.; Nov 20th, 2024 at 01:10 PM.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Messagebox in form load event hangs splash screen

    Quote Originally Posted by .paul. View Post
    Your problem is because MessageBoxes are modal. As soon as you show the MessageBox, all of your other code is stopped waiting for the MessageBox to close. There are 2 possible fixes. 1st is to show the MessageBox in a secondary thread, or 2nd, something similar to Red.x’s Timer idea…

    Edit: Possibly, there’s an overload of MessageBox.Show that allows you to choose a parent or an owner, which would place it in front of the specified form…
    There is an Owner overload, but when I use the splash form as the owner, it gives me cross-thread operation error. This is the code I tried:

    Code:
    MessageBox.Show(My.Forms.Splash1, "Test")
    How would I show the messagebox in a secondary thread?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Messagebox in form load event hangs splash screen

    Code:
    Dim t As New Thread(
        Sub()
            MessageBox.Show("Test")
        End Sub)
    t.Start()

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Messagebox in form load event hangs splash screen

    The issue is that the splash screen won't be dismissed until the startup form is ready to be displayed but the startup form is not ready to be displayed until it's Load event handler has been executed. Displaying the message on a different thread is not the solution because then it's not modal to the startup form anyway, so it won't stop the startup form being displayed. The obvious solution is to display the message in the Shown event handler instead of Load, as long as displaying the message immediately after the startup form is displayed rather than just before is OK. If that's a problem, you simply cannot use the standard splash screen functionality and have this form be the startup form. There are ways to achieve what you want but they will require more work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Messagebox in form load event hangs splash screen

    How about putting what ever is in the message box in a label on the splash screen and save the user and extra click?
    Please remember next time...elections matter!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,110

    Re: Messagebox in form load event hangs splash screen

    I was going to go roughly in the direction Tyson talked about, but then thought better of it.

    I ran into an issue that a standard splash screen wouldn't cover. In my case, the splash screen didn't stay visible long enough. In fact, the Shown event of the main form wasn't long enough, either. I needed the splash screen to remain visible (and full screen) for about two seconds after the Shown event of the main form was raised. That's not a typical splash screen, so I had to make my own...sort of. In reality, something that is "like a splash screen, but not like a splash screen" is not a splash screen.

    What I did was create a full-screen window that was shown non-modally. The main form was created in the background and displayed, with the façade (splash screen) able to handle events from the main form. Of course, that façade screen was just a form, and could have whatever else you wanted on it. A spinner would be dull and misleading, so, aside from some graphical elements, the main feature of the façade screen was a message that was periodically updated to let the user know that progress was being made.

    You could do something like that, with a message showing whatever you wanted while the main form is in the background...doing whatever you want it to do. You might want to be showing that message from before the Load event of the main form, too. Show it as soon as possible.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width