13
votes

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.

2

2 Answers

27
votes
    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();
    }
1
votes

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();
}