Winform Forms Closing and opening a new form

后端 未结 8 1226
时光说笑
时光说笑 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:01

    In program.cs:

    void Main() {
      frmLogin fl = new frmLogin();
      if (fl.ShowModal() == DialogResult.Ok) {
        frmHome fh = new frmHome();
        fh.Show();
      }
    }
    
    0 讨论(0)
  • 2020-12-06 08:01

    try that it might help you

    frmHome frm = new frmHome();
    this.hide()
    frm.ShowDialog();
    this.Close();
    
    0 讨论(0)
  • 2020-12-06 08:03

    I'm not quite sure I'm understanding this, perhaps you could do something like this if you want to loop through a specific order of forms: (pseudo code)

    Dim formsList as New List(Of form)
    formsList.add(Form1)
    formsList.add(Form2)
    formsList.add(Form3)
    ' etc etc etc '
    
    For Each f as Form in formsList
    
        f.ShowDialog()
        ' you could have a condition here which breaks the for loop perhaps '
    
    Next
    
    Me.close ' close the app '
    

    You could add a condition into the For loop which breaks the for loop to end early...

    note: sorry for the vb.net code...but it should be easy to understand though

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

    Why don't you just stop calling application.exit in the form_closed event?

    I am not sure that you really need it anyway, and if you do then you can remove the x icon from the screen and give them a close button.

    Alternatively there is a CloseReason in the event args that will tell you if it is a user closing the form or code or something else.

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

    One way to accomplish this:

    1. Open the main form on startup.
    2. Hide it. (optional really, but not for you if you really can't show more than one form.)
    3. Open your login form with ShowDialog();
    4. If login is successful, then show your main form. If not, then close your main form / the application.
    0 讨论(0)
  • 2020-12-06 08:13

    you can force the form to hide, instead of close. instead of catching the form_closed event, catch the form_closing event. it would look something like this.

    private void LoginFrm_Closing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
        frmHome frm = new frmHome();
        frm.Show();
    }
    

    this will keep both open, but only one visible. somewhere in frmHome, possibly a public variable to hold the LoginFrm, so you can toggle between the two with Hide(); and Show(); (and any other forms you may wish to add)

    Edit: Grammar.

    0 讨论(0)
提交回复
热议问题