简体   繁体   English

如何在C#中循环多个复选框

[英]how loop through multiple checkbox in C#

I have 100 checkboxes in a winform.我在 winform 中有 100 个复选框。 Their names are sequential like checkbox1 , checkbox2 etc. I have a submit button in my winform.它们的名称是连续的,如checkbox1checkbox2等。我的 winform 中有一个提交按钮。 After clicking the submitting button, it checks, if a checkbox is checked then some value is updated otherwise another value is updated.单击提交按钮后,它会检查,如果选中了复选框,则更新某个值,否则更新另一个值。 I have to check 100 checkbox.我必须选中 100 复选框。 So i have to loop through the 100 checkbox to check if the checkbox is checked or not.所以我必须遍历 100 复选框来检查复选框是否被选中。

I know how to check the checkbox我知道如何选中复选框

private void sumit_button_Click(object sender, EventArgs e)
{
     if (checkbox1.Checked)
     { 
        //  update 
     }
     else
     {  
        // update another  
     }

     if (checkbox2.Checked)
     {  
        //  update    
     }
     else
     {   
        // update another  
     }

     ......................and so on

} 
        

But how can i do this for 100 checkbox???但是我怎样才能为 100 个复选框做到这一点???

foreach (var control in this.Controls) // I guess this is your form
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                        //update
                    }
                    else
                    {
                        //update another
                    }
                }
            }
foreach (var ctrl in panel.Controls) {
    if (ctrl is CheckBox && ((CheckBox)ctrl).IsChecked) {
        //Do Something
    }
}
foreach (var box in this.Controls.OfType<CheckBox>())
{
    if (box.Checked)
    {
        //...
    }
    else
    {
        //...
    }
}

There is LINQ method OfType .有 LINQ 方法OfType Why not use it to get rid of manual type testing and casting?为什么不使用它来摆脱手动类型测试和铸造?

foreach (var ctrl in panel.Controls.OfType<CheckBox>().Where(x => x.IsChecked)
{
    // ....
}
    foreach (Control childc in Page.Controls)
    {

            if (childc is CheckBox)
            {
                CheckBox chk = (CheckBox)childc;
                //do your operation

            }

    }

This is the write answer for this................这是这个问题的书面答案......

c# C#

           string movie="";
           if (checkBox1.Checked == true)
            {
                movie=movie+checkBox1.Text + ",";
            }
            if (checkBox2.Checked == true)
            {
                movie=movie+checkBox2.Text + ",";
            }
            if (checkBox3.Checked == true)
            {
                movie=movie+checkBox3.Text + ",";
            }

            if (checkBox4.Checked == true)
            {
                movie = movie + checkBox4.Text + ",";
            }
            if (checkBox5.Checked == true)
            {
                movie = movie + checkBox5.Text + ",";
            }
            if (checkBox6.Checked == true)
            {
                movie = movie + checkBox6.Text + ",";
            }
          row["EnquiryFor"] = movie.ToString();

where row is a object of DataRow and EnquiryFor is the name of sql table column....其中 row 是 DataRow 的对象,EnquiryFor 是 sql 表列的名称....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM