【问题标题】:Assign checkboxes to an checkbox-array using a for loop in C#在 C# 中使用 for 循环将复选框分配给复选框数组
【发布时间】:2014-10-25 15:44:11
【问题描述】:

我想这样做:

CheckBox[] boxarray = new CheckBox[4];
    boxarray[0] = checkBox1;
    boxarray[1] = checkBox2;
    boxarray[2] = checkBox3;
    boxarray[3] = checkBox4;

使用 for 循环 - 因为我有大约 600 个复选框。我想到了这个:

for (int i = 0 ; i<= 600; i++)
    {
    boxes[i] = checkBox[i] ;
    }

但无论我尝试了什么 - 它都没有奏效。我希望你知道我的意思。 感谢您的帮助!

【问题讨论】:

  • 您是在 Visual Studio 设计器中创建这些复选框吗?
  • 因为我有大约 600 个复选框我很想看看 UI 的样子。
  • 你的意思是你有大约 600 个条目要放在一个复选框中吗?或者你真的想创建 600 个复选框?对我来说似乎很极端!!!
  • 这是一个网络表单应用程序吗?表格? wpf?
  • 什么是boxes[]checkBox[] 是什么?

标签: c# arrays loops for-loop checkbox


【解决方案1】:

如果您的表单上有这些复选框,那么您可以像这样将它们添加到数组(或列表,这样做会容易得多):

List<CheckBox> checkBoxesList = new List<CheckBox>();
foreach (Control ctrl in FormName.Controls)
{
    if (ctrl.GetType() == typeof(CheckBox))
    {
        checkBoxesList.Add(ctrl as CheckBox);
    }
}

记住,只需写:
checkBox1, checkBox2
不会像checkBox[0] 那样得到它们 - 它不是一个数组,它只是一个最后带有 '1' 或 '2' 的名称。

【讨论】:

    【解决方案2】:

    使用当前设置,您必须使用 FindControl(假设 ASP.net、winforms 和 WPF 工作方式不同):

    for(int i = 0; i <= 600; i++)
    {
        boxes[i] = Page.FindControl("checkbox" + i);
    }
    

    windows 窗体更新:实际上有一种方法可以用来查找特定类型的所有控件:

    How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

    这是您的另一个选择。我通过创建样本对其进行了测试 应用程序,然后我将一个 GroupBox 和一个 GroupBox 放在初始 组框。在嵌套的 GroupBox 内,我放置了 3 个 TextBox 控件和一个 按钮。这是我使用的代码(甚至包括你之前的递归) 寻找)

    public IEnumerable<Control> GetAll(Control control,Type type) {
        var controls = control.Controls.Cast<Control>();
        return controls.SelectMany(ctrl => GetAll(ctrl,type))
                       .Concat(controls)
                       .Where(c => c.GetType() == type); 
    }
    

    为了在表单加载事件中测试它,我想要计算里面的所有控件 初始组框

    private void Form1_Load(object sender, EventArgs e) {
        var c = GetAll(this,typeof(TextBox));
        MessageBox.Show("Total Controls: " + c.Count());
    }
    

    它每次都返回正确的计数,所以我认为这对 你在找什么:)

    如果您不想在表单上添加其他复选框,则必须使用以下方法:

    for(int i = 0; i <= 600; i++)
    {
        boxes[i] = this.Controls.Find("checkbox" + i, true).FirstOrDefault() as CheckBox;
    }
    

    【讨论】:

    • 其实我没有。我只是假设。除了我知道如何最好地解释它的技术之外,我没有任何逻辑理由假设它是 ASP.NET。不过,这个概念保持不变:他必须将他的计数器连接到始终保持不变的部分并手动查找它们。
    • 更新了 Winform 方法,因为这是 OP 使用的方法。
    【解决方案3】:

    您无法像之前那样在代码中指定复选框的名称,因为i 不会被更改。

    您必须创建 CheckBox 的新实例,例如:

    for (int i = 0 ; i<= 600; i++)
    {
        //This will create a new instance of a CheckBox
        CheckBox cb = new CheckBox()
    
        //At this point the checkbox is empty and not visible anywhere
        //So you have to change some properties:
        cb.Text = "Check me!";
        cb.Top = Ycoord; //you should change this depending on i like +(i*30)
        cb.Left = Xcoord;
        cb.name = "checkbox" + i.toString();
        //To recognize if one of your Checkboxes is checked/unchecked you need to add
        //an event like this (for the event see down below)
        cb.CheckedChanged += checkedChangedEvent;
        //then you need to add the checkbox to your Array
        boxes[i] = cb;
        //to make those checkboxes visible you need to add them to your form
        //or Panel:
        myForm.Controls.Add(cb);
    }
    

    事件:

    public void checkedChangedEvent(Object sender, EventArgs e)
    {
        MessageBox.Show(((CheckBox)sender).name + " is now Checked == " + ((CheckBox)sender).Checked.toString();
    }
    

    您必须通过名称指定哪个复选框被选中/取消选中,因为每个 CheckBox 都在触发相同的事件。

    EDIT2:

    要获取复选框的编号,您可以在事件中执行以下操作:

    int cbNumber = 0;
    try
    {
        cbNumber = System.Convert.toInt32(((CheckBox)sender).name.Replace("checkbox",""));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
    }
    

    【讨论】:

    • 我也读到过。但实际上我以前从未这样做过,所以我什至不确定这会是什么样子。或许你能解释一下它的作用?
    • 添加了更多细节 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2019-05-29
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多