Jun 14, 2011

How Do I Check/Uncheck all CheckBoxes on a Form


private void chkSelectall_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSelectall.Checked == true)
            {
                MessageBox.Show("checked");
check check boxes:

                CheckBox chkbox;
             
                foreach (Control c in this.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Checked;
                     
                    }
                }



else
            {
           
uncheck check boxes:
                MessageBox.Show("not cheked");


                CheckBox chkbox;
                foreach (Control c in this.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Unchecked;

                    }
                }


if you have any panel or group box add this code also

uncheck check boxes:

 foreach (Control c in grpLogFiles.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Unchecked;

                    }
                }
check check boxes:


 foreach (Control c in grpLogFiles.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Checked;

                    }
                }






7 comments:

  1. One more way to check all is:

    private void FillControlsMultipleSelectedValues(Control ctl, string value)
    {
    try
    {
    if (!string.IsNullOrEmpty(value))
    {

    string[] separatedValues = value.Split('|');

    if (ctl is CheckedListBox)
    {
    CheckedListBox tempCLB = ctl as CheckedListBox;

    for (int i = 0; i < tempCLB.Items.Count; i++)
    {
    if (separatedValues.Contains(tempCLB.Items[i].ToString()))
    {
    tempCLB.SetItemChecked(i, true);
    }
    }
    }

    if (ctl is Panel)
    {
    foreach (CheckBox item in ctl.Controls.OfType())
    {
    if (separatedValues.Contains(item.Text))
    {
    item.Checked = true;
    }
    }
    }

    if (ctl is Panel)
    {
    foreach (RadioButton item in ctl.Controls.OfType())
    {
    if (item.Text.StartsWith(separatedValues[0]))
    {
    item.Checked = true;
    }
    }
    }
    }
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }

    ReplyDelete
  2. To call the above method use it as below:

    FillControlsMultipleSelectedValues(this.cblChiefComplaint, CCHPIdt.Rows[0]["ChiefComplaint"].ToString());

    ReplyDelete
  3. To Clear you may use below:

    while (cblChiefComplaint.CheckedIndices.Count > 0)
    {
    cblChiefComplaint.SetItemChecked(cblChiefComplaint.CheckedIndices[0], false);
    }

    ReplyDelete
  4. Glad it helped! :)

    ReplyDelete