How to check all CheckBoxes on Form


You can check all checkboxes on the form whether they are checked or not.

How To Check All CheckBoxes on Form



Suppose you have more than 100 Checkboxes on your form. And when you submit that form you want to check whether these all Checkbox have been checked by the user or not. But here you can't implement if...else condition to check these all 100 Checkboxes. So for implementation of that I have used looping concept.

Here is the code snippet for this:
I have only used four checkbox and checked them. You can include more checkbox and this would work fine. Set the loop condition to number of checkbox on that form.

private void btnSave_Click(object sender, EventArgs e)
{
string strConcatenate;
int iCheckCounter=0;

for (int i = 0; i < 4; i++)
{
iCheckCounter++;
strConcatenate = "checkBox" + iCheckCounter.ToString();

Control[] arrControl= this.Controls.Find(strConcatenate, false);
CheckBox chk = (CheckBox)arrControl[0];

if (chk.Checked == true)
{
lblMessage.Text += " " +chk.Text + " Checked\n";
}
else
{
lblMessage.Text += " " + chk.Text + " Unchecked\n";
}


}
}


But here is the limitation on Checkbox that they need to be named same such as checkBox1, checkBox2 and so on.


Attachments

  • checkBox_Codes (37627-9919-dns_Checkbox.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: