Why all that? OK, just use the method
System.Windows.Forms.Form.Close
.
That's not all. The problem is a little bit less trivial.
The show the same form again (just use
Form.Show
, not matter if the form was closed before or not), you need to prevent form disposal. This is done via handling the event
Form.FormClosing
, .
this.FormClosing += (sender, evenArgs) => {
if (evenArgs.CloseReason != CloseReason.ApplicationExitCall) {
evenArgs.Cancel = true;
this.Hide();
}
};
Alternatively, override the method
System.Windows.Forms.Form.OnFormClosing
to get the same effect. The check of
CloseReason
is needed to allow regular action on closing of the application.
Important: If this trick is applied to the main form as well, it can create the following problem: how to close the application? Well, you will need to add a control or a menu item to do that; it should call
Application.Exit
explicitly.
Overall, the style of UI you're trying to implement is pretty bad: not only it's harder to implement; it is also confusing the users. Consider the following: you use just one main window, but it changes its appearance because it is a parent of 3 panels filling the form client area; you show only one panel at a time. Alternatively, create just a tabbed UI based on
System.Windows.Forms.TabControl
— this is the easiest and a very natural way.
—SA