using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SplashProject
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
/* a new static timer instance, it's important for starting and handeling the time during it the splash form is displayed*/
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
// Counter is an integer that help us to fix the number of seconds during them the Splash form is displayed
static int counter = 0;
// b is a boolean related to the event if the Spalsh form will be disposed or not
static bool b = false;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/*-----------------The modification of the void Main method behavior----------------*/
// New instance of Form2
Form2 oForm2 = new Form2();
//Add event handler to myTimer.Tick event
myTimer.Tick+=new EventHandler(myTimer_Tick);
//Fix myTimer interval to 1000 witch is 1 second
myTimer.Interval = 1000;
//Start myTimer
myTimer.Start();
/* This is the conditional loop during it the Form2 will appear as Splash form */
while (b == false)
{
//Display oForm2
oForm2.Show();
/*This is very important to add this line of code because it is responsable for
dispalying oForm2 during the time provided to this action*/
Application.DoEvents();
if (b == true)
{
/*Disposing oForm2 when the time is out */
oForm2.Dispose();
/*Going out of this while loop*/
break;
}
Application.Run(new Form1());
}
}
/*This is the surcharged method that handels myTimer.Tick*/
static void myTimer_Tick(Object sender, EventArgs e)
{
// Stop the timer
myTimer.Stop();
/* Here we chose 4 seconds or a little more to dispaly the splash form oForm2, and then to dispose it */
if (counter < 4)
{
// Enable the counter
myTimer.Enabled = true;
// Increment the counter
counter++;
}
// The condition related to witch the Spalsh form will be disposed
if (counter == 4) b = true;
}
}
}
In deed, if you want to precise the position and the size of your Splash-Form programmatically, you can implement the Form2_Load(Object sender, EventArgs e) {} according to two ways and you can chose one among those mentioned below:
//The first way
private void Form2_Load(object sender, EventArgs e)
{
this.Location = new Point(200, 200);
this.Size = new Size(300, 300);
}
//The second way
private void Form2_Load(object sender, EventArgs e)
{
this.Top = 200;
this.Left = 200;
this.Width = 300;
this.Left = 300;