My Solution:
So I managed to find another tutorial http://www.codeproject.com/KB/dotnet/Yet_Another_Splash_Screen.aspx and the sourcecode seemed to make more sense to me. Here is the code i'm using now. Main()
is left untouched.
Splash.cs
`
public partial class Frm_Splash : Form { delegate void ProgressDelegate(int percent); delegate void SplashShowCloseDelegate();
/// <summary> /// To ensure splash screen is closed using the API and not by keyboard or any other things /// </summary> bool CloseSplashScreenFlag = false; /// <summary> /// Base constructor /// </summary> /// public Frm_Splash() { InitializeComponent(); progress_Splash.Show(); this.ClientSize = this.BackgroundImage.Size; } public void ShowSplashScreen() { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen)); return; } this.Show(); Application.Run(this); } /// <summary> /// Closes the SplashScreen /// </summary> public void CloseSplashScreen() { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen)); return; } CloseSplashScreenFlag = true; this.Close(); } /// <summary> /// Update text in default green color of success message /// </summary> /// <param name="Text">Message</param> public void Progress(int percent) { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new ProgressDelegate(Progress), new object[] { percent }); return; } // Must be on the UI thread if we've got this far progress_Splash.Value = percent; // Fade in the splash screen - looks pro. :D if (percent < 10) this.Opacity = this.Opacity + .15; } /// <summary> /// Prevents the closing of form other than by calling the CloseSplashScreen function /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SplashForm_FormClosing(object sender, FormClosingEventArgs e) { if (CloseSplashScreenFlag == false) e.Cancel = true; } }`
Form1.cs
public partial class Frm_Main : Form { Frm_Splash frm_Splash = new Frm_Splash(); public Frm_Main() { this.Hide(); Thread splashthread = new Thread(new ThreadStart(frm_Splash.ShowSplashScreen)); splashthread.IsBackground = true; splashthread.Start(); InitializeComponent(); CenterToScreen(); } private void Frm_Main_Load(object sender, EventArgs e) { if (PassedAll() == true) FillMovieLB(); if (FillMovieProgress == 100) { //Throw in this sleep so the user can see the progress bar reach all the way to the end. Thread.Sleep(1000); this.Show(); frm_Splash.CloseSplashScreen(); this.Activate(); } }
Original Question
G'day all,
I'm very new to programming in C# and i'm having a problem with the http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx tutorial and implementing it within my application. I'm finding it a little difficult to understand what the problem is. I know there is alot of stuff about getting this splash screen to work but I can't get my head around it.
When I start the program, the Frm_Main
will display, you can see the listbox
being populated, because i've placed it in BackgroundWorker.DoWork()
, and then afterwards my frm_Splash
will show after the work is done. Obviously, the way it should be working is, frm_Splash
will show during the work being done on Frm_Main
, and the progress bar will show the progress of the loading (this part I haven't implemented yet).
Edit: I may not have been clear, but the question is: How can I get my splashscreen to display while the work is being done and before the main form is displayed?
Thanks everybody. :)
Here is my code:
static Frm_Splash frm_Splash = new Frm_Splash();
public delegate void ShowFormDelegate();
public void ShowForm()
{
frm_Splash.Show();
}
public Frm_Main()
{
InitializeComponent();
CenterToScreen();
if (PassedAll() == true)
{
back_loadprog.RunWorkerAsync();
}
}
private void back_loadprog_DoWork(object sender, DoWorkEventArgs e)
{
Invoke(new ShowFormDelegate(ShowForm));
Invoke(new FillMovieLBDelegate(FillMovieLB));
}