【问题标题】:Fill checkbox array names from strings从字符串中填充复选框数组名称
【发布时间】:2012-06-08 23:05:40
【问题描述】:

我正在尝试使用从字符串派生的复选框名称填充复选框数组。我想替换以下代码:

    public void CheckBox_1()
    {
        CheckBox[] boxes_1 = new CheckBox[4];

        boxes_1[0] = A0;
        boxes_1[1] = A1;
        boxes_1[2] = A2;
        boxes_1[3] = A3;

        for (int i = 0;i < 4;i++)   
        {
            boxes_1[i].Enabled = checkBox1.Checked == true ? true : false;
        }
    }

类似这样的:

    public void CheckBox_1()
    {
        CheckBox[] boxes_1 = new CheckBox[4];

        for (int i = 0; i < 4; i++)
        {
            boxes_1[i] = ("A" + i);
        }

        for (int i = 0; i < 4; i++)
        {
            boxes_1[i].Enabled = checkBox1.Checked == true ? true : false;
        }
    }

我可以很容易地将复选框名称转换为字符串,但不清楚如何完成此操作。谢谢。

【问题讨论】:

  • 将复选框放在列表中会更好。你这样做的方式需要反思。
  • 你可以使用反射......但你应该澄清你的最终目标。也许我们可以提供更好的方法来完成这项工作。
  • 相关的复选框是否以某种方式组合在一起(例如 GroupBox)?
  • "boxes_1[i].Enabled = checkBox1.Checked == true ? true : false;"因此,如果 true 为 true,则返回 true ...或者如果 false 不是 true,则返回 false?三元if有什么原因吗?

标签: c# arrays string checkbox


【解决方案1】:

您可以使用包含对象的Control.Controls 通过OfType&lt;T&gt; 获取所有CheckBox 控件,然后过滤以“A”开头的Names。

var container = ...control with the checkboxes...;

foreach(var cb in container.Controls.OfType<CheckBox>().Where(c => c.Name.StartsWith("A")))
{
   cb.Enabled = checkBox1.Checked;
}

【讨论】:

    【解决方案2】:

    与其摆弄反射只是为了创建容易出错且难以理解的代码,我建议将相关复选框分组到容器控件中,例如GroupBox

    那么它就非常简单易读了:

    // consider renaming
    public void CheckBox_1()
    {
        var relatedCheckBoxes = GroupBox1.Controls.OfType<CheckBox>();
        foreach (var chk in relatedCheckBoxes)
            chk.Enabled = checkBox1.Checked; // you might want to pass this checkbox as argument instead
    }
    

    【讨论】:

      【解决方案3】:

      假设复选框可以嵌套在某个公共容器内的其他控件中,这可能会起作用:

          private void CheckBox_1()
          {
              foreach (var checkbox in FindChildren<CheckBox>(container, c => c.Name.StartsWith("A")))
              {
                  checkbox.Enabled = checkBox1.Checked == true ? true : false;
              }
          }
      
          public static IEnumerable<T> FindChildren<T>(Control parent, Func<T, bool> filter)
              where T : Control
          {
              var search = new Stack<Control>();
              search.Push(parent);
      
              while (search.Count > 0)
              {
                  parent = search.Pop();
      
                  foreach (Control child in parent.Controls)
                  {
                      T typed = child as T;
      
                      if (typed != null && filter(typed))
                      {
                          yield return typed;
                          continue;
                      }
      
                      search.Push(child);
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-21
        • 2019-01-07
        • 2013-06-06
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多