In this article I am trying to make a splash screen in C# windows form.
Splash Screen:
The splash screen is an introduction page that is displayed as a program is loading. We can also say splash screen is boot screen or welcome screen. For example, When SQL Server is starting up, there is a windows splash screen is displayed while it’s loading.
Below I am making a demo example for splash screen with Backgroundworker:
1. Create a new project in visual studio 2010.
2. By default form1 is show in visual studio working area; you can also add a second form which is making splash screen.
3. In second form (Form2) set the FormBorderStyle: None. As below image.
4. And after that make as you wish design for your Form2. Below my designed Form2:
Using Backgroundworker
Backgroundworker makes threads easy to implement in Windows Forms. Deep tasks need to be done on another thread so that the UI doesn't freeze. It is necessary to post messages and update the user interface when the task is done.
How to implement Backgroundworker in my demo project:
1. Drag and drop the Backgroundworker from the toolbox.
Coding of my demo:
Form2 _f2;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
_f2 = new Form2();
}
private void Form1_Load(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
_f2.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
}
private void btnCancle_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 100; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_f2.Close();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
}
Output:-
Rahul Kesharwani
23-May-2020Great tutorial. Thanks for Good Information