1

I have a problem regarding the loading screen I've created.

I execute the code run but after the progress bar is finished, the form is shown but closes automatically.

Why is this happening?

namespace LogIn 
{

    public partial class Loading : Form
    {
        public Loading()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(2);
            if (progressBar1.Value == 100) timer1.Stop();
            if (progressBar1.Value == 100)
            {
                this.Close();
                Login Login = new Login();
                Login.ShowDialog();

            }
        }
    }
}
dash
  • 89,546
  • 4
  • 51
  • 71
Yukimoto Otomikuy
  • 101
  • 2
  • 3
  • 8
  • 2
    What do you think that `this.Close()` is doing? – Steve Jun 06 '13 at 17:08
  • If you are showing Loading as your main application form then, when you call close, it closes the form, and hence ends the application. In your main, you probably have the line 'Application.Start(Loading)' or similar. – dash Jun 06 '13 at 17:08
  • oh!! thank you!! i use it as my main form! thank you!! now i know that if main form(this.close();) is similar to Application.Exit(); THANKYOU! – Yukimoto Otomikuy Jun 06 '13 at 17:12
  • I don't think they are simliar, this.Close() just ends the message loop of the form Loading, it still runs in the static method Main, if there are more code in that method (after the Application.Run(new Loading())), the code will be executed while Application.Exit() will terminate the application at once. They are different. =)) – King King Jun 06 '13 at 19:23
  • @KingKing You are absolutely correct in that they aren't the same - you are right, too, any code after the Application.Run will execute before the application ends. The *effects* are similar, though, in the usual case (i.e. no code after the Application.Run). – dash Jun 06 '13 at 21:40

1 Answers1

1

In your startup code (your Main method), you probably have something like the following:

Application.Run(new Loading());

This makes Loading your main Application Form. When you call Close, you are closing Loading, which effectively ends the application.

What you are really looking for is the concept of a Splash Screen.

See this question and related answers for an overview.

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71