Winform Forms Closing and opening a new form

后端 未结 8 1227
时光说笑
时光说笑 2020-12-06 07:47
1. frmHome frm = new frmHome();
   frm.Show();
   this.Close();

I\'m opening HomeForm from LoginForm. In LoginForm

相关标签:
8条回答
  • 2020-12-06 08:15

    All you need to do is this it closes the 2 forms without problem. Form1 fin = new Form1(); fin.Close(); this.Visible = false; Form2 win = new Form2(); win.Visible = true;

    0 讨论(0)
  • 2020-12-06 08:22

    you can use a boolean (global variable) as exit flag in LoginForm

    initialize it to :

    exit = true;//in constructor
    

    set it to false before closing:

    frmHome frm = new frmHome();
    frm.Show();
    exit = false;
    this.Close();
    

    and in form_closed:

    if(exit) Application.Exit();
    

    if a user closes the form with the 'X' button, exit will have the value true, and Application.Exit() will be called.

    EDIT:

    the above is not working because LoginForm is your main form used by Application.Run(loginForm).

    2 suggestions:

    With exit flag:

    replace

    Application.Run(new LoginForm())
    

    by

    LoginForm loginFrm = new LoginForm();
    loginFrm.Show();
    Application.Run();
    

    Without exit flag:

    replace in your current code:

    frmHome frm = new frmHome();
    frm.Show();
    this.Close();
    

    by

    frmHome frm = new frmHome();
    this.Visible = false;
    frm.ShowDialog();
    this.Close();
    
    0 讨论(0)
提交回复
热议问题