是否可以迭代按钮并设置可见性? [英] Is it possible to iterate through buttons and set visibility?

查看:83
本文介绍了是否可以迭代按钮并设置可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有20个按钮的winform。加载表格是否可以这样:

  for (i =  1 ; i< =  20 ; i ++)
if ( somevalue == 1
{
button(i).Visible = true ;
}
else
{
button(i).Visible = ;
}

somevalue是对Excel中值的引用。



所以我想要基于匹配值可见或隐藏的按钮。此匹配值存储在外部(excel)文档中。



我尝试过:



基本上我想设置按钮对用户设置的可见性,但是我无法管理。我在C#中寻找正确的代码,但仍然没有任何成功。

解决方案

是:

  foreach (控制c 控件中)
{
按钮b = c as 按钮;
if (b!= null
{
b。 Visible = someValue == 1 ;
}
}

如果您的按钮位于容器(如Panel)中,那么您可能只需要检查panel.Controls集合,或者递归所有c.Collections以查找它们。


I have a winform with 20 buttons. Is something like this possible on loading form:

for(i=1; i<=20; i++)
    if(somevalue == 1)
    {
        button(i).Visible = true;
    }
    else
    {
        button(i).Visible = false;
    }

"somevalue" is a reference to a value in Excel.

So I want buttons visible or hidden based on a matching value. This matching value is stored in an external (excel) document.

What I have tried:

Basically I want to set visibility of buttons to user settings but that I can not manage. I am looking for the correct code in C# but still without any succes.

解决方案

Yes:

foreach (Control c in Controls)
   {
   Button b = c as Button;
   if (b != null)
      {
      b.Visible = someValue == 1;
      }
   }

If your buttons are within a container such as a Panel, then you may need to check just the panel.Controls collection, or recurse all c.Collections to find them.


这篇关于是否可以迭代按钮并设置可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆