I am trying to make the splash screen appears first and after the splash, the MainForm
appears. But the progress bar which I have in splash screen don't get to the end of the bar. And the program continues running and not works.
How can I show the splash screen during loading the main form?
My code It's something like that :
public partial class SplashForm : Form
{
public SplashForm()
{
InitializeComponent();
}
private void SplashForm_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
timer1.Interval = 1000;
progressBar1.Maximum = 10;
timer1.Tick += new EventHandler(timer1_Tick);
}
public void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value != 10)
{
progressBar1.Value++;
}
else
{
timer1.Stop();
Application.Exit();
}
}
}
Here are the first part of the code of the MainForm
:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
Application.Run(new SplashForm());
}
}
There are different way of creating splash screens. It's better to separate the logic of showing and closing splash screen from logic of your main form.
To do so, you can create a LoadCompleted
event and then subscribe for it in Program
class, and show and close your splash screen there.
Here is an implementation of what I described above:
1- In your MainForm
, add a LoadCompleted
event and then override OnLoad
method to raise the event. (Probably Shown
event is applicable instead of our custom event.)
public event EventHandler LoadCompleted;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.OnLoadCompleted(EventArgs.Empty);
}
protected virtual void OnLoadCompleted(EventArgs e)
{
var handler = LoadCompleted;
if (handler != null)
handler(this, e);
}
private void MainForm_Load(object sender, EventArgs e)
{
//Just for test, you can make a delay to simulate a time-consuming task
//In a real application here you load your data and other settings
}
2- In the Program
class, show SplashForm
then subscribe for LoadCompleted
event of your MainForm
and show MainForm
, then in LoadCompleted
, close SplashForm
.
static class Program
{
static SplashForm mySplashForm;
static MainForm myMainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Show Splash Form
mySplashForm = new SplashForm();
if (mySplashForm != null)
{
Thread splashThread = new Thread(new ThreadStart(
() => { Application.Run(mySplashForm); }));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
}
//Create and Show Main Form
myMainForm = new MainForm();
myMainForm.LoadCompleted += MainForm_LoadCompleted;
Application.Run(myMainForm);
if(!(mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed))
mySplashForm.Invoke(new Action(() => {
mySplashForm.TopMost = true;
mySplashForm.Activate();
mySplashForm.TopMost = false; }));
}
private static void MainForm_LoadCompleted(object sender, EventArgs e)
{
if (mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed)
return;
mySplashForm.Invoke(new Action(() => { mySplashForm.Close(); }));
mySplashForm.Dispose();
mySplashForm = null;
myMainForm.TopMost = true;
myMainForm.Activate();
myMainForm.TopMost = false;
}
}
来源:https://stackoverflow.com/questions/32418695/show-splash-screen-during-loading-the-main-form