Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
There are two forms

Form A ( Parent )
Form B ( Child )

1. The Problem is to make Form B as Parent and Form A as Child when Form B shows.
2. When Form B shows up i want Form A to close.

i haved tried but unable to fix this problem, has this problem got anything to do with threading?
Please help!
Posted
Comments
BobJanova 8-Nov-11 10:45am    
Why don't you just show Form B to start with? Is this a splash screen or similar situation? (in which case you should search CP for splash screen solutions)
Sergey Alexandrovich Kryukov 8-Nov-11 11:11am    
Just a bad idea. Please see my solution.
--SA
Sergey Alexandrovich Kryukov 8-Nov-11 11:11am    
Why doing that?!
--SA

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
C#
class FormMain : Form {
    void PromoteToMainForm() {
        PromotedAsMainForm = new //...
        Application.Exit(); //important, it won't terminate your process, see Main below
    } 
    internal Form PromotedAsMainForm;
}


Then, your Main can use it:
C#
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
 
Share this answer
 
v6
Comments
BobJanova 8-Nov-11 11:26am    
I'm not sure he actually means 'parent/child' in the sense of using the Parent property.

And there are sometimes good reasons for multiple forms, for example multiple similar documents or something like that which requires a full UI for each one (Microsoft set the precedent for that), floating tool windows that need to be visible at the same time as some main content, not to mention modal dialogues.

The most common use though is for a splash screen or login screen which requires a particular solution, which is why I asked the OP if that's his intent.
Sergey Alexandrovich Kryukov 8-Nov-11 11:39am    
Of course he did not mean that, but should I explain it or not?
I did not deny multiple form designs all together, so I agree with you, but in many cases the opportunity of multiple forms is heavily abused, I just know.
--SA
Sergey Alexandrovich Kryukov 8-Nov-11 11:40am    
Splash screen is a good example of the usage of this stuff.
By the way, you cannot deny I provided the solution which would work for a splash screen, can you? So, what's the problem?

Thank you for your notes.
--SA
Sergey Alexandrovich Kryukov 8-Nov-11 13:27pm    
So, on second though, I decided to re-phrase the last paragraph in more accurate way and credit you for your notes.
Thanks again!
--SA
Menon Santosh 8-Nov-11 13:16pm    
Nice Solution my 5
Should FormA be your main form (the one mentioned in Program.cs
Application.Run(new YourMainFormNameHere()); ), you can still close it. But that will close all other forms, too.
Try hiding FormA instead of closing it.
Or make a totally different Form your main form (not FormA, not FormB). Then you can close and re-open instances of FormA and FormB without restrictions.d
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Nov-11 11:11am    
There is another method; and a real advice would be to avoid this bad idea.
Please see my solution.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900