Hide all open forms

I have three forms.

Suppose A, B, C.

Form A opens Form B and Form B then opens Form C.

I added a Hide all open forms button in C.

Now, how can I hide all three forms with this button?

I know that one way uses ShowWindow Api, but I don't want to use Api calls.

Edit: Thanks SoMoS .

for (int i = Application.OpenForms.Count - 1; i >= 0; i += -1) { if (!object.ReferenceEquals(Application.OpenForms[i], this)) { Application.OpenForms[i].Hide(); } } this.Hide(); 

or

In form A (thanks to ho1 )

 B frm = new B(); frm.Owner = this; frm.Show(); 

In the form of B

 C frm = new C(); frm.Owner = this; frm.Show(); 

In the button click event of form C.

 Owner.Owner.Hide(); Owner.Hide(); Hide(); 

Or thanks to Wim Cohenen

 foreach (Form var in Application.OpenForms) { var.Hide(); } 

Thanks.

+7
c #
source share
4 answers

You just need to access this collection:

 Application.OpenForms 

Then you just need to iterate over all the elements and hide the ones you want (you can check by name, for example) or just hide all of them.

Hope this helps.

+7
source share

It works:

 Owner.Owner.Hide(); Owner.Hide(); Hide(); 

Or, if you are not sure how many forms will be in the chain, you can simply have a recursive method.

Although it depends on the fact that A is the owner of B, etc., which you can organize by sending this as a parameter for calls to Show when you show the forms.

+3
source share

Instead of hiding all forms, you can use the fact that minimizing the form automatically minimizes all child forms. Therefore, when C.Owner = B, B.Owner = A, you can simply use (in the Click handler in A):

 WindowState = FormWindowState.Minimized 
0
source share
 Form2 NewForm = new Form2(); this.Hide(); //Hide Current form NewForm..ShowDialog(); //Show new form this.Show(); //Show Previous form After close new form 
-2
source share

All Articles