Sunday, June 5, 2011

Add a Splash Screen to Your C# Windows Application Project

How to create a splash screen in C#

Tutorial: How to create a splash screen in C#
Time: 5 minutes
Experience needed: Basic understanding of Threads and the flow of applications

In this tutorial you will learn how to create an application that displays a splash form for x seconds.
The benefits for this are: if you have a form that takes a while to load up (for insastance one that does a lot of Database interaction) the user will sit there wondering why your app is not open yet.

With a splash screen you can give the user something pretty to look at to keep them happy.

From here on in I assume you have a created a new project/solution and/or a new form.

Step 1 - Setup

If you are in Design View press F7 or click View then Code to swith to code view.

At the top of the source code you will see lines of code similar to this

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Add the following line to that code

Code:
using System.Threading;
You should now have something that looks like this

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Threading;
Step 2 - Setting up the splash screen

Add a new Windows Form to your application. This will be your splash screen. I will leave you alone for 5 minutes so you can make it jazzy!

(5 minutes later).

Welcome back. So, we have our splash form, we have our main form. Now we need to tell our program we want to open our splash screen before our main screen.

In the source code of your main form, look for code like this

Code:
public Form1()
        {
            InitializeComponent();
         }
Here we are going to create a new Thread which will trigger the creation and showing of our splash form. Type the following code below InitializeComponent().

Code:
Thread t1 = new Thread(new ThreadStart(SplashForm));
            t1.Start();
            Thread.Sleep(5000); // The amount of time we want our splash form visible
            t1.Abort();
            Thread.Sleep(1000);
Now we should have code that looks like this

Code:
public Form1()
        {
            InitializeComponent();

            Thread t1 = new Thread(new ThreadStart(SplashForm));
            t1.Start();
            Thread.Sleep(5000); // 5 seconds
            t1.Abort();
            Thread.Sleep(1000);
        }
One thing may be puzzling you there, whats SplashForm? SplashForm is the method that will open our splash form. Heres the code for that.

Code:
private void SplashForm()
        {
            SplashForm newSplashForm = new SplashForm();
            newSplashForm.ShowDialog();
            newSplashForm.Dispose();
        }
So when the thread starts, the above code wil run.

And there you have it, you have made a splash screen in C#.

FYI: There are several ways this could have been done, I used Threads as I was already using them for another part of a project. Another popular way would be to use a timer.

Happy Programming.

No comments:

Post a Comment

Feel the Pulse of Tech' !!!

Feel the Pulse of Tech' !!!
Ur's may stop, but this will Beat 4 EVER