我的应用程序中有多个工具条控件,并且正在寻找一种方法来一次隐藏它们。
例如
allToolStrips.Visible = false;
代替
toolstrip1.Visible = false;
toolstrip2.Visible = false;
...
toolstripn.Visible = false;
如果重要,我正在使用 C#。
容易的
foreach(Control ctrl in this.Controls)
{
if(ctrl.GetType() ==typeof(ToolStrip))
ctrl.Visible=false;
}
将它们放在一个向量中,然后将它们隐藏在每个循环中?
您可以使用 linq 来完成。像这样的东西。
this.Controls.Select(c => c is ToolStrip).ToList().ForEach(ts => ts.Visible = false);
我没有检查语法,但我认为没关系。
除了其他人的答案之外,请考虑对其进行编码,以便在切换控件时也可以使用相同的代码将控件再次翻转为可见,这样您就不会重复代码:
void SetMenusVisibility(bool visible)
{
//credit to Vivek for his loop
foreach(Control ctrl in this.Controls)
{
if(ctrl.GetType() ==typeof(ToolStrip))
ctrl.Visible=visible;
}
}