1

我的应用程序中有多个工具条控件,并且正在寻找一种方法来一次隐藏它们。

例如

allToolStrips.Visible = false;

代替

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

如果重要,我正在使用 C#。

4

4 回答 4

5

容易的

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

         ctrl.Visible=false;    

}
于 2010-02-01T09:59:19.380 回答
2

将它们放在一个向量中,然后将它们隐藏在每个循环中?

于 2010-02-01T09:59:10.813 回答
1

您可以使用 linq 来完成。像这样的东西。

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

我没有检查语法,但我认为没关系。

于 2010-02-01T09:59:11.223 回答
0

除了其他人的答案之外,请考虑对其进行编码,以便在切换控件时也可以使用相同的代码将控件再次翻转为可见,这样您就不会重复代码:

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

             ctrl.Visible=visible;    

    }
}
于 2010-02-01T10:17:45.123 回答