Results 1 to 8 of 8

Thread: Splash Screen [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Resolved Splash Screen [RESOLVED]

    Hello,

    I am trying to build a splash screen for my application. So far I have in my Startup sub:

    Code:
                MyThread = New Threading.Thread(AddressOf ShowSplashScreen)
                MyThread.Name = "Splash Screen"
                MyThread.Start()
    And the sub:

    Code:
        Public Sub ShowSplashScreen()
            Splash = New SplashForm
            Splash.Show()
            'Only when I put MessageBox.Show("Hello") does this form show
        End Sub
    And in the main form load event:

    Code:
    Splash.Close()
    Why is it that the splash form only shows when I show a message box?
    Can anybody tell me what I am doing wrong?
    Last edited by cpatzer; Apr 20th, 2005 at 12:48 PM.
    In life you can be sure of only two things... death and taxes. I'll take death.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Splash Screen

    Probably because you're terminating the new Thread as soon as your form is loaded.

    There are a number of way to do this, one tried and true method is to simply use a Timer, i.e.

    Splash Form:
    VB Code:
    1. Public Class frmSplash
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    33.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    34.       Me.components = New System.ComponentModel.Container
    35.       Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    36.       '
    37.       'frmSplash
    38.       '
    39.       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    40.       Me.ClientSize = New System.Drawing.Size(292, 266)
    41.       Me.Name = "frmSplash"
    42.       Me.Text = "frmSplash"
    43.  
    44.    End Sub
    45.  
    46. #End Region
    47.  
    48.    Private Sub frmSplash_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    49.       Timer1.Interval = 5000 ' Approx. 5 Seconds.
    50.       Timer1.Enabled = True
    51.    End Sub
    52.  
    53.    Private Sub frmSplash_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
    54.       Me.Close()  ' Close when the Splash Screen is Clicked
    55.    End Sub
    56.  
    57.    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    58.       Timer1.Enabled = False
    59.       Me.Close()  ' Close the Splash Screen after the time has elapsed
    60.    End Sub
    61. End Class
    Main Form:
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    33.       '
    34.       'Form1
    35.       '
    36.       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    37.       Me.ClientSize = New System.Drawing.Size(292, 266)
    38.       Me.Name = "Form1"
    39.       Me.Text = "Form1"
    40.  
    41.    End Sub
    42.  
    43. #End Region
    44.  
    45.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    46.       Dim splash As New frmSplash   ' Create a new Splash Form Instance
    47.       Me.AddOwnedForm(splash)       ' Make the Splash Form a Child Form of this Form (so it appears over it)
    48.       splash.Show()                 ' Display the Splash Form
    49.    End Sub
    50. End Class
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: Splash Screen

    The reason for that is that my application is rather large and requires quite a bit of time to load on slower machines. What I am looking to do is something like Adobe's Acrobat Reader or Photoshop. Load the splash screen first in new thread while main form is background loading. Then when the main form is loaded it closes the splash screen. Any ideas?

    Thanks.
    In life you can be sure of only two things... death and taxes. I'll take death.

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Splash Screen

    You can use the code I posted, just remove the Timer and close the form after doing eveything you do in your forms Load event.

    You my need to make a call to Application.DoEvents after showing the splash.

    You don't need to start a new Thread to accomplish this.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.       Dim splash As New frmSplash
    3.       Me.AddOwnedForm(splash)
    4.       splash.Show()
    5.  
    6.       Dim i As Integer
    7.       For i = 1 To 1000
    8.          Console.WriteLine("Do Something")
    9.       Next
    10.  
    11.       splash.Close()
    12.    End Sub
    Regards,

    - Aaron.

  5. #5

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: Splash Screen

    That doesn’t quite do what I am looking for though as the load method is called after all of the dll's have been loaded and all of the form layout code has been hashed through.

    This is the part that takes the time, not any routine that I have of my own.

    That's why I was trying to do it with a separate thread in the startup sub so that the splash screen gets launched before any other code is executed, thus spanning the bridge in time between user double clicking on the program and the application actually loading.

    Thanks.
    In life you can be sure of only two things... death and taxes. I'll take death.

  6. #6

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Unhappy Re: Splash Screen {UNRESOLVED}

    Hate to do this but still no luck with this. Does anybody have any idea how to create a splash screen simlar to Microsoft Outlook or Adobe Reader...?

    It needs to load first before any other code is executed or parsed.

    Thanks.
    In life you can be sure of only two things... death and taxes. I'll take death.

  7. #7
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Splash Screen

    Try this instead:

    In your "Splash Screen" Form Add the Following:
    VB Code:
    1. Private Shared _SplashScreen As frmSplashScreen
    2.  
    3.    Public Shared Sub ShowSplashScreen()
    4.       If Not _SplashScreen Is Nothing Then
    5.          CloseSplashScreen()
    6.       End If
    7.  
    8.       _SplashScreen = New frmSplashScreen
    9.       _SplashScreen.Show()
    10.    End Sub
    11.  
    12.    Public Shared Sub CloseSplashScreen()
    13.       If Not _SplashScreen Is Nothing Then
    14.          Try
    15.             _SplashScreen.Close()
    16.             _SplashScreen = Nothing
    17.          Catch
    18.  
    19.          End Try
    20.       End If
    21.    End Sub
    Assuming your splash screen form is called frmSplashScreen you'd make
    your startup object Sub Main which would be in a module:
    VB Code:
    1. Module modStartup
    2.    Sub Main()
    3.       frmSplashScreen.ShowSplashScreen()
    4.       Application.Run(New Form1) ' Where "Form1" is the main form that takes a while.
    5.    End Sub
    6. End Module
    To close the SplashScreen, do so in at the end of the main form's
    Load event, i.e.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.       For i As Integer = 1 To 5000
    3.          ' Simulate a load delay
    4.          Console.WriteLine(i)
    5.       Next
    6.       frmSplashScreen.CloseSplashScreen()
    7.    End Sub
    Regards,

    - Aaron.

  8. #8

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Resolved Re: Splash Screen

    Aaron,

    Ok great, I had to make some changes but was able to get it to work. Your example worked except that the SplashForm would not show any controls just white space where the controls should have been. I was able to get around this by using ShowDialog instead of Show in the ShowSplashScreen sub.

    This though introduced a new problem, code execution stopped in the StartUp sub. To get around this I started a new thread:

    VB Code:
    1. SplashScreenThread = New System.Threading.Thread(AddressOf SplashScreen.ShowSplashScreen)
    2.             SplashScreenThread.Start()

    This works great and accomplished just what I needed.

    Thank you for your help!!!


    **Edit**

    If you use this you will also need to bring to front the main form after the SplashScreen form closes.
    VB Code:
    1. Private Sub SplashScreen_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         MainForm.BringToFront()
    3.     End Sub
    Last edited by cpatzer; Apr 20th, 2005 at 12:46 PM.
    In life you can be sure of only two things... death and taxes. I'll take death.

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