ImonBayazid
ImonBayazid

Reputation: 1096

how loop through multiple checkbox in C#

I have 100 checkboxes in a winform. Their names are sequential like checkbox1, checkbox2 etc. I have a submit button in my 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. So i have to loop through the 100 checkbox to check if the checkbox is checked or not.

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

Upvotes: 5

Views: 22246

Answers (6)

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

Upvotes: 0

Display Name
Display Name

Reputation: 8128

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)
{
    // ....
}

Upvotes: 3

Joel Coehoorn
Joel Coehoorn

Reputation: 415820

foreach (var box in this.Controls.OfType<CheckBox>())
{
    if (box.Checked)
    {
        //...
    }
    else
    {
        //...
    }
}

Upvotes: 2

heq
heq

Reputation: 412

foreach (var control in this.Controls) // I guess this is your form
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                        //update
                    }
                    else
                    {
                        //update another
                    }
                }
            }

Upvotes: 8

Dhaval
Dhaval

Reputation: 2861

    foreach (Control childc in Page.Controls)
    {

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

            }

    }

Upvotes: 1

coder
coder

Reputation: 13248

foreach (var ctrl in panel.Controls) {
    if (ctrl is CheckBox && ((CheckBox)ctrl).IsChecked) {
        //Do Something
    }
}

Upvotes: 5

Related Questions