Splash Screen until a form loads.

G

Guest

Now I got some input a day or two ago, and I have spent the evening trying to
implement it. I have had no luck.

I need a Splash Screen to open, and run a progress bar. I need, while it
does that, for the main form to load.

I am using the Application.DoEvents in the Splash form's Load event. The
problem is that for some reason the splash screen starts, then the other form
opens up, then it goes back to the splash screen.

Currently I have the progress bar with a timer and max value 100, but how
can I have the main form tell the splash screen it is done?

Here is what I need specificly (I am seldom very clear):

frmStart will open
frmStart will tell frmMain to open
frmStart will begin scrolling a progress bar so user knows something is
happening.
frmStart will some how magically know that frmMain is completely loaded.
frmStart will.
 
G

Guest

How about having a separate 'main' class that will be the entry point to your
app. This will first run the splash screen and when it is done, you can show
your actual form.

Regards,
Kumar
 
G

Guest

Sorry, I always leave something out on my questions. Yes, I tried that
article. It is not in VB (which is what I forgot to state). I guess I will
try the class option. Thanks for the input.
 
G

Guest

I was having a terrible problem getting a Splash screen to show until it was
done and then showing the Main form. Using some ideas from this thread
[http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/casoast.asp]
I was able to solve my problems. I will do my best to copy/paste my code in
hopes that it will help.

The following code contains 3 classes (2 of which are Forms [1 Main/1 Splash])
I will omit the Designer code and any unnecessary code.

'Apploaded.vb - Entry point for the application
Public Class AppLoader
Private Shared mForm As MainForm = New MainForm

<STAThread()> _
Shared Sub Main(ByVal args() As String)
Application.Run(mForm)
End Sub
End Class

'MainForm.vb
Imports System.Threading

Public Class MainForm
Inherits System.Windows.Forms.Form
'<Omitted code here>
Shared splash As SplashScreen = New SplashScreen
Const iMinAmountOfSplashTime As Integer = 5

Public Sub StartSplash()
' Run the form
Application.Run(splash)
End Sub

Private Sub CloseSplash()
If (splash Is Nothing) Then Return

' Shut down the splash screen
splash.Invoke(New EventHandler(AddressOf splash.KillMe))
splash.Dispose()
splash = Nothing
End Sub

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim bShowSplash As Boolean = True
bShowSplash = RegistryAccess.GetStringRegistryValue("Show Splash",
TypeCode.Boolean, True)
If (bShowSplash) Then
' Create a new thread from which to start the splash screen form
Dim otter As New ThreadStart(AddressOf Me.StartSplash)

Dim splashThread As Thread = New Thread(otter)
splashThread.ApartmentState = ApartmentState.STA
splashThread.Start()

' Pretend that our application is doing a bunch of loading and
' initialization
Thread.Sleep(iMinAmountOfSplashTime / 2)
splash.Show()

' Loop until the Splash Screen is done
While (splash.IsRunning())
'My application has an animation
'Application.DoEvents() is used to
'to make sure the Animation is shown
Application.DoEvents()
End While
End If
' Close the splash screen
CloseSplash()
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If (Not splash Is Nothing) Then Return
MyBase.OnPaint(e)
End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
If (Not splash Is Nothing) Then Return
MyBase.OnPaintBackground(pevent)
End Sub

Protected Overrides Sub OnClosing(ByVal e As
System.ComponentModel.CancelEventArgs)
' Make sure the splash screen is closed
CloseSplash()
MyBase.OnClosing(e)
End Sub
End Class

'SplashScreen.vb
'The m_bDone is set to True when the following conditions are met:
' a. The Splash Screen has finished loading
' b. The user clicks on the link to begin (not shown)
Public Class SplashScreen
Inherits System.Windows.Forms.Form
'<Omitted code here>
Private m_bDone As Boolean = False

'Called by MainForm::Load (Event)
Public Function IsRunning() As Boolean
Return Not m_bDone
End Function

'Called by MainForm::CloseSplash
Public Sub KillMe(ByVal o As Object, ByVal e As EventArgs)
'Dispose any graphics or other objects here
Me.Close()
End Sub
End Class

Thats pretty much it. The project uses the AppLoaded class as the start up
object (via Main). The original thread used a timer and mose of the code can
easily be ported to VB.NET. I hope this helps. Any question please reply or
email me at (e-mail address removed) with "Splash Screen" as the subject.

Thanks and best wishes!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top