Like it

Sunday, July 17, 2011

A Simple Splash screen in C#

I started my .net career in VB.Net. While developing a windows application in VB.Net it is pretty easy to put a Splash screen. There is Project property called Splash screen and you can put any form there, after that the specified form will act as a Splash screen. But when I started an windows application in C#, there is no project property called Splash screen. While doing some searching, I found lot of ways, like Putting a timer and closing the splash etc. But I feel like it is not the right way of doing a splash screen, then I started my own implementation and found one. I am not sure it is good one or not, but it is working for me.

Here is the steps:

1) Added one Form with an Image, as Splashscreen.cs, and my main UI is Welcome.cs
2) In the Program.cs I modified the code like the following

[STAThread]
static void Main()
{
    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    SplashScreen s = new SplashScreen();
    s.Show();
    Welcome welcome = new Welcome(s);
    Application.Run(welcome);
}

3) And in the Welcome.cs, added one parameterized constructor, with SplashScreen as the Parameter

SplashScreen _s = null;
public Welcome(SplashScreen s)
{
    InitializeComponent();
    this._s = s;
}

4) And in the Form_Load() event I added code to close the splash screen if exists.

if (this._s != null) { this._s.Close(); }

5) Run the application, see the splash is coming and after that Main UI is coming

No comments:

Post a Comment