开发者

.NET Referencing similar controls

开发者 https://www.devze.com 2022-12-18 11:54 出处:网络
开发者_开发技巧I have multiple toolstrip controls in my application and was looking for a way to hide them all at once.
开发者_开发技巧

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.


easy one

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

         ctrl.Visible=false;    

}


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


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.


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;    

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消