First of all, there is not such relationship as child-parent forms.
[EDIT: There are MDIParent and MDIChildren, but this is not exactly the same thing. Also, I don't think we discuss MDI. Thanks to CS2011 who pointed out that relationship between forms in case of MDI.]
There are Owned Forms and Owner Forms. You really need to use this relationship; it affect activation behavior (all forms of the same application will open activate together). You need to use
System.Windows.Forms.Form.Owner
. It's good to make all forms owned by the main form and set
Form.ShowInTaskbar
to
false
for all owned form, by the apparent reason.
Now, of you need to open, close and reopen non-main (as we hopefully agree, owned forms), you need to implement the following behavior: hide form instead of closing:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
Hide();
}
}
Pay attention for check up of
CloseReason
: the behavior will be applied only to attempt to close the form by the user.
The show can be lazy, but regardless to the show state of the form does not matter, if can be currently shown or not:
MyForm MyForm;
if (MyForm == null) {
MyForm = new MyForm();
this.AddOwnedForm(MyForm);
MyForm.ShowInTaskbar = false;
}
MyForm.Show();
—SA