简体   繁体   中英

How can I open another form after splash screen in C#?

I am new to c# and I am using windows forms.

I have 3 Forms :

  • SpalshScreenForm with progressBar and Timer

  • MainForm

  • ResultForm

What I am trying to do is: I want to write if statement in SplashScrreenForm which checks if Example.txt file exists or not while progressBar is loading, and if the file exists I want to open MainForm and if it doesn't exist I want to open ResultForm .

As shown in code I run the program the SpalshScreenForm opens and after that it closes and then MainForm opens. The issue is that ResultForm never opens and it seems that the if statement does not get executed.

My question is : how can I open SplashScreenForm and check if the files exists, if the file exists I want to open MainForm and if it doesn't exist I want to open ResultForm . I am happy to listen to any new suggestions/ideas. Please help me thank you.

Program.cs Code:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

MainForm Code:

public MainForm()
    {

        Thread t = new Thread(new ThreadStart(Splash_Screen));
        t.Start();
        Thread.Sleep(3000);

        InitializeComponent();         

        t.Abort();

    }
 public void Splash_Screen()
    {
        Application.Run(new SpalshScreenForm());
    }

SplashScreenForm Code:

 public SplashScreenForm()
    {           
        InitializeComponent();
    }


    private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(1);
        if(progressBar1.Value==100)
        {

            if (!File.Exists(@"E:\Example.txt"))
                {

                  ResultForm _ResultForm= new _ResultForm();
                  _ResultForm.ShowDialog();
                }

            timer1.Stop();            


        }
    }

In Program.cs , change the last line to

Application.Run(new SplashScreenForm());

To make sure the Splash Screen is the startup project.

Next, change the if statement that checks for the existence of Example.txt in the SplashScreenForm code to

if (!File.Exists(@"E:\Example.txt"))
{
    ResultForm _ResultForm= new _ResultForm();
    _ResultForm.ShowDialog();
}
else
{
    MainForm _MainForm = new _MainForm();
    _MainForm.Show();
}

This ensures that either the ResultForm or the MainForm is shown at when the splash screen disappears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM