C# Opening a new form and closing the other one

前端 未结 5 498
深忆病人
深忆病人 2021-01-12 07:03

In my program I show a login form before the main form, once the program detects a successful login I use this:

MainForm Main = new MainForm();
Main.Show();
         


        
5条回答
  •  生来不讨喜
    2021-01-12 07:47

    You need to specify your MainForm when you staring application and in the Load event handler of this form ask for login. In this case you will have runned application and Login for on the starting:

    Program.cs

        Application.Run(new MainForm());
    

    MainForm.cs

        private void Form1_Load(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm();
            if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // check login here
            }
        }
    

    P.S. Close will close your application completelly if it's main form of your application.

提交回复
热议问题