C# Opening a new form and closing the other one

前端 未结 5 527
深忆病人
深忆病人 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:54

    Try something more like this:

    this.Hide();
    Main.ShowDialog();
    this.Close();
    

    You want to hide the login form before you show the dialog, then close the login form after the dialog has been closed.

    Simply closing the Login dialog will ultimately end the application, so that's not a real solution, but you still want to hide the login.

    Simply put, put things in the order you want them to go in, especially when dealing with message loops.

    First, you hide the login form.
    Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed.
    Last, once the dialog is closed, you close the login form, ending the application.

提交回复
热议问题