views:

55

answers:

4

I have multiple toolstrip controls in my application and was looking for a way to hide them all at once.

E.g.

allToolStrips.Visible = false;

instead of

toolstrip1.Visible = false;
toolstrip2.Visible = false;
...
toolstripn.Visible = false;

I'm using C# if it matters.

+1  A: 

You can do it using linq. Something like this.

this.Controls.Select(c => c is ToolStrip).ToList().ForEach(ts => ts.Visible = false);

I haven't checked the syntax, but I think it's ok.

klausbyskov
I'm not very good in LINQ, but this `.ToList().ForEach()` looks a little awkward and i there exists a better way to change a property of all items within an enumeration.
Oliver
+2  A: 

Put them in a vector and then hide them in a for each loop?

PoweRoy
+5  A: 

easy one

foreach(Control ctrl in this.Controls)
{           
         if(ctrl.GetType() ==typeof(ToolStrip))

         ctrl.Visible=false;    

}
Vivek Bernard
A: 

In addition to other's answers, consider coding it so that same code can also be used to flip the controls back to visible again if you are toggling them, so that you don't have the code duplicated:

void SetMenusVisibility(bool visible)
{
    //credit to Vivek for his loop
    foreach(Control ctrl in this.Controls)
    {           
             if(ctrl.GetType() ==typeof(ToolStrip))

             ctrl.Visible=visible;    

    }
}
AaronLS