C# 如何遍历表单上的所有复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1788490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:50:01  来源:igfitidea点击:

How can I iterate through all checkboxes on a form?

c#winformscheckbox

提问by NinjaCat

I have a form that has many dynamically generated checkboxes. At runtime, how can I iterate through each of them so I can get their value and IDs?

我有一个包含许多动态生成的复选框的表单。在运行时,我如何遍历它们中的每一个,以便获得它们的值和 ID?

采纳答案by Michal Franc

foreach(Control c in this.Controls)
{
   if(c is CheckBox)
   {
   // Do stuff here ;]
   }
}

回答by harryovers

When they are created, get a list of references to the values, and then you can iterate over the list.

创建它们时,获取对值的引用列表,然后您可以遍历该列表。

回答by Max Galkin

Like this, maybe (if it's in Windows Forms):

像这样,也许(如果它在Windows Forms 中):

foreach(var checkBox in myForm.Controls.OfType<CheckBox>())
{   
   //Do something.
}

回答by Adriaan Stander

If it is Windows Forms, you can try something like this:

如果是Windows Forms,您可以尝试这样的操作:

private void button1_Click(object sender, EventArgs e)
{
    Dictionary<string, bool> checkBoxes = new Dictionary<string, bool>();
    LoopControls(checkBoxes, this.Controls);
}

private void LoopControls(Dictionary<string, bool> checkBoxes, Control.ControlCollection controls)
{
    foreach (Control control in controls)
    {
        if (control is CheckBox)
            checkBoxes.Add(control.Name, ((CheckBox) control).Checked);
        if (control.Controls.Count > 0)
            LoopControls(checkBoxes, control.Controls);
    }
}

Remember that container controls can contain children, so you might want to check those too.

请记住,容器控件可以包含子控件,因此您可能也需要检查这些控件。

回答by Robert Jeppesen

I use a simple extension method that will work for any control type:

我使用一种适用于任何控件类型的简单扩展方法:

  public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
  {
     bool hit = startingPoint is T;
     if (hit)
     {
        yield return startingPoint as T;
     }
     foreach (var child in startingPoint.Controls.Cast<Control>())
     {
        foreach (var item in AllControls<T>(child))
        {
           yield return item;
        }
     }
  }

Then, you can use it like so:

然后,您可以像这样使用它:

var checkboxes = control.AllControls<CheckBox>();

Using IEnumerable lets you choose how to store the results, and also lets you use linq:

使用 IEnumerable 可以让您选择如何存储结果,还可以让您使用 linq:

var checkedBoxes = control.AllControls<CheckBox>().Where(c => c.Checked);

回答by Allan Ramirez

I know that this is old, but It was easy as I can imagine.

我知道这很旧,但正如我想象的那样容易。

Just add all checkboxes into a List<Checkbox>, all checkboxes state are in the list and even if they change in the UI in the list changes too.

只需将所有复选框添加到 a 中List<Checkbox>,所有复选框状态都在列表中,即使它们在列表中的 UI 中更改也会更改。

List<Checkbox> checkboxes = new List<Checkboxes>();
checkboxes.Add(chk1);
checkboxes.Add(chk2);
//So add all checkboxes you wanna iterate

foreach(Checkbox checkbox in checkboxes){
    //Do something using checkbox object
}

Hope this helps :)

希望这可以帮助 :)