how loop through multiple checkbox in C#

前端 未结 6 1040
后悔当初
后悔当初 2021-01-14 00:06

I have 100 checkbox in a winfrom. Their name is sequential like checkbox1,checkbox2 etc. I have a submit button in my winform. After clicking the submitting button, it check

相关标签:
6条回答
  • 2021-01-14 00:34

    There is LINQ method 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)
    {
        // ....
    }
    
    0 讨论(0)
  • 2021-01-14 00:38
        foreach (Control childc in Page.Controls)
        {
    
                if (childc is CheckBox)
                {
                    CheckBox chk = (CheckBox)childc;
                    //do your operation
    
                }
    
        }
    
    0 讨论(0)
  • 2021-01-14 00:40
    foreach (var box in this.Controls.OfType<CheckBox>())
    {
        if (box.Checked)
        {
            //...
        }
        else
        {
            //...
        }
    }
    
    0 讨论(0)
  • 2021-01-14 00:47
    foreach (var ctrl in panel.Controls) {
        if (ctrl is CheckBox && ((CheckBox)ctrl).IsChecked) {
            //Do Something
        }
    }
    
    0 讨论(0)
  • 2021-01-14 00:50
    foreach (var control in this.Controls) // I guess this is your form
                {
                    if (control is CheckBox)
                    {
                        if (((CheckBox)control).Checked)
                        {
                            //update
                        }
                        else
                        {
                            //update another
                        }
                    }
                }
    
    0 讨论(0)
  • 2021-01-14 00:51

    This is the write answer for this................

    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....

    0 讨论(0)
提交回复
热议问题