Home >Backend Development >C++ >How to Properly Sequence Form Opening and Closing in C#?
How to Sequentially Open and Close Forms in C#
When working with multiple forms in a C# application, it is often necessary to open a new form and then close the current one. This can be achieved through the following steps:
Opening a New Form:
Closing the Current Form:
Contrary to Steve's solution, calling this.Close() immediately will also close the newly opened form2. To correctly close the current form, it is recommended to hide it and focus on form2 instead.
private void OnButton1Click(object sender, EventArgs e) { this.Hide(); var form2 = new Form2(); form2.Closed += (s, args) => this.Close(); form2.Show(); }
In this code:
This approach ensures that the current form is not closed until form2 is closed, maintaining the proper sequence of form transitions.
The above is the detailed content of How to Properly Sequence Form Opening and Closing in C#?. For more information, please follow other related articles on the PHP Chinese website!