Even though the class
System.Windows.Forms.Form
has the properties
System.Windows.Forms.Control.Controls
and
System.Windows.Forms.Control.Parent
, the
parent-child relationship between forms is not really operational.
(More exactly, if you try to make one form a parent of another, an exception will be thrown. You can avoid it by assigning the property
TopLevel = false
for a child form and make another form its parent, but the result is ugly: one form sitting inside the client area of another form as a control. You should never do it.)
So, by the reason explained in the previous paragraph,
forget about child-parent relationship between forms and never remember it.
Now, this has nothing to do with threading. Moreover, you can only call any methods/properties of the UI from the same UI thread. To access UI from other threads you need to use UI thread
invocation, but this is a separate topic. What is funny though — you can run two or more independent UIs in different threads, but this is an advanced topic and hardly can be advisable. For this moment, forget threading.
Now, what you want to do with forms is really bad style. You should remember one form in the UI is the main form. It's defined by what parameter was used in
Application.Run
(normally, this code is placed in your application entry method, normally
Main
). If you close a main form, the application exits. You can use it running application with other form. You can use this fact.
Create some method in the main form called
PromoteToMainForm
class FormMain : Form {
void PromoteToMainForm() {
PromotedAsMainForm = new
Application.Exit();
}
internal Form PromotedAsMainForm;
}
Then, your
Main
can use it:
static void Main() {
FormMain firstMain = new FormMain();
Application.Run(firstMain);
Form secondMain = firstMain.PromotedAsMainForm;
if (secondMain != null)
Application.Run(secondMain);
}
Alternatively, hide main form instead closing it. Make sure you leave the user the opportunity to exit the application, for example, via a menu of all forms.
[EDIT]
What you really need to use, is another relationship between forms: Owner/Owned form. If you really need to use multiple forms (but see below), do yourself a big favor: make all forms owned by the main form. It will support your application integrity: when you activate any form, all the forms of your application will go on top certain Z-order the way that all the windows will be Z-grouped, without any other window placed in Z-order between any of the forms of your application. For the similar benefit, always set
ShowInTaskbar = false
for non-main forms.
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar.aspx[
^].
[END EDIT]
Now, a real advice:
think thoroughly if you really need this. Even though such schema can be used in certain cases (thank you for your notes, Bob), they are really rare. In most real-life situations, you should do something really different. Don't even use multiple forms, use only one, main form. Add some container control(s) to is and show/hide them to make your form look different, use tab control, etc. There are many good designs.
—SA