Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms switching between forms

Tags:

winforms

I'm using winforms now. I have the main form "form1" and I have a button that opens form2. When I open form2 I would like for form1 to disappear. When the user click the x button on form2 I would like for it to close and go back to form1. I wouldn't like to use modal windows.

like image 367
Itay.B Avatar asked May 01 '11 11:05

Itay.B


2 Answers

    private void button1_Click(object sender, EventArgs e) {
        var frm = new Form2();
        frm.Location = this.Location;
        frm.StartPosition = FormStartPosition.Manual;
        frm.FormClosing += delegate { this.Show(); };
        frm.Show();
        this.Hide();
    }
like image 199
Hans Passant Avatar answered Oct 24 '22 18:10

Hans Passant


In order not to change Form's Properties, simply use the ShowDialog() method in Form1.cs code to open Form2. That will deactivate Form1:

void OpenSecondForm()
{
  Form form2 = new Form();
  form2.ShowDialog();
}
like image 35
YuriArtem Avatar answered Oct 24 '22 17:10

YuriArtem