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;
}
}
Thanks!
ReplyDeleteOne more way to check all is:
ReplyDeleteprivate 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);
}
}
To call the above method use it as below:
ReplyDeleteFillControlsMultipleSelectedValues(this.cblChiefComplaint, CCHPIdt.Rows[0]["ChiefComplaint"].ToString());
To Clear you may use below:
ReplyDeletewhile (cblChiefComplaint.CheckedIndices.Count > 0)
{
cblChiefComplaint.SetItemChecked(cblChiefComplaint.CheckedIndices[0], false);
}
Thq Thakkar..
ReplyDeleteGlad it helped! :)
ReplyDeleteThx Namrata..
ReplyDelete