Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding all open forms

Tags:

c#

I have three forms.

Lets say A, B, C.

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

I have added button Hide all open forms in form C.

Now how do I hide all three forms with this button ?

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

Edit : Thanks to 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 form B

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

In form C's button click event.

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

Or thanks to Wim Coenen

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

Thanks.

like image 312
Searock Avatar asked Nov 17 '10 08:11

Searock


1 Answers

You just need to access this collection:

Application.OpenForms

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

Hope it helps.

like image 121
Ignacio Soler Garcia Avatar answered Nov 15 '22 10:11

Ignacio Soler Garcia