Switch Winforms between Forms

Now I use winforms. I have the main form "form1" and I have a button that opens form2. When I open form 2, I would like form 1 to disappear. When the user presses the x button on form2, I would like it to close and return to form1. I would not want to use modal windows.

+8
winforms
source share
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(); } 
+18
source share

To not change the properties of the form , simply use the ShowDialog() method in Form1.cs to open Form2 . This will disable Form1 :

 void OpenSecondForm() { Form form2 = new Form(); form2.ShowDialog(); } 
0
source share

All Articles