Thursday, May 8, 2008

Main Form and Splash Screen c# windows application

Splash screen in Visual Studio is a greatway to give user the feedback of loading the application.Problem I faced implementing the Splash Screen is main form always used to hide in the task bar.After doing a couple of searches I found out that spash screen delay time is the culprit.

I made the splash screen to show for few seconds than my main form which gives the focus to the main form that avoids the form staying in the task bar.

In Application Events.vb I added the following

Namespace My
' The following events are availble for MyApplication:
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
'Set the splash screen timeout.
Me.MinimumSplashScreenDisplayTime = 1000
Return MyBase.OnInitialize(commandLineArgs)
End Function
'End code paste
End Class
End Namespace

In the form load is the following code

Dim dt As DateTime = DateTime.Now
Me.WindowState = FormWindowState.Normal
Dim timespaninms As Integer = (DateTime.Now - dt).Milliseconds
''Comment or delete the line below once you get the average load time after 5 or 6 runs.
'MessageBox.Show("Time in MilliSeconds : " + timespaninms.ToString)
If (timespaninms < My.Application.MinimumSplashScreenDisplayTime) Then
My.Application.MinimumSplashScreenDisplayTime = timespaninms
My.Application.SaveMySettingsOnExit = True
timespaninms = My.Application.MinimumSplashScreenDisplayTime - timespaninms
End If

No comments:

Post a Comment