.NET 引用类似控件

发布于 2024-08-19 16:10:31 字数 257 浏览 5 评论 0原文

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

例如,

allToolStrips.Visible = false;

如果

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

重要的话,我会使用 C#。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-08-26 16:10:31

简单的一个

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

         ctrl.Visible=false;    

}

easy one

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

         ctrl.Visible=false;    

}
白况 2024-08-26 16:10:31

将它们放入向量中,然后将它们隐藏在 foreach 循环中?

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

梦年海沫深 2024-08-26 16:10:31

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

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

我没有检查语法,但我认为这是可以的。

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.

娇纵 2024-08-26 16:10:31

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

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

             ctrl.Visible=visible;    

    }
}

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;    

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文