5

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

4
  • Foreach control c in form.controls. Then if c is CheckBox Commented Sep 13, 2013 at 5:07
  • @DanielAbouChleih-Do you have all checkboxes inside a container? Commented Sep 13, 2013 at 5:09
  • you may consider a datagrid control instead in which you will have a checkbox column and some other invisible column to hold the common logic for each checkbox. Then you can iterate through the rows of the datagrid. Commented Sep 13, 2013 at 5:12
  • Is there any relationship between the data to be updated and the checkbox, or can you creat an relationship? Commented Sep 13, 2013 at 5:14

6 Answers 6

8
foreach (var control in this.Controls) // I guess this is your form
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                        //update
                    }
                    else
                    {
                        //update another
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

That's it. What did you mean with "Do you have all checkboxes inside a container?"?
inside a container means a panel or a group box ?
Correct, but as + null check is better than is + cast (because you only have to cast once). Even better would be Controls.OfType<CheckBox>()
But why do ask that me?
Note that this is C# .NET 4 code and not compatible with older .NET
3

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

2 Comments

Someone did bring it up actually :)
Oops... didn't notice
2
foreach (var box in this.Controls.OfType<CheckBox>())
{
    if (box.Checked)
    {
        //...
    }
    else
    {
        //...
    }
}

2 Comments

How can i get the checkbox number to use inside the if clause. @Joel Coehoorn
@DarkenShooter You don't need it. You have a direct reference to the checkbox. If you do need that number, you're doing something wrong with your design.
1
    foreach (Control childc in Page.Controls)
    {

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

            }

    }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.