Reputation: 25
My code only doing if chckAll.Checked than make all the check boxes selected... What I want to achieve is when any of the check boxes unselected, after selecting all the check boxes make the chckAll unselected.... Also If all the check boxes selected by one by than make the chckAll selected...How can I do that?
private void chckAll_CheckedChanged(object sender, EventArgs e)
{
if (chckAll.Checked)
{
foreach (Control ctrl in checkBoxesPanel3.Controls)
{
CheckBox chkboxes = ctrl as CheckBox;
if (chkboxes != null)
{
chkboxes.Checked = true;
}
}
}
}
Upvotes: 1
Views: 391
Reputation: 63065
Add below event to all other checkboxes except chckAll
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckFlg = true;
if (!CheckAllFlg)
{
chckAll.Checked = checkBoxesPanel3.Controls.OfType<CheckBox>().Where(x => x.Name != "chckAll").All(c => c.Checked);
}
CheckFlg = false;
}
private void chckAll_CheckedChanged(object sender, EventArgs e)
{
CheckAllFlg = true;
if (!CheckFlg)
{
foreach (CheckBox ctrl in checkBoxesPanel3.Controls.OfType<CheckBox>().Where(x => x.Name != "chckAll"))
{
ctrl.Checked = chckAll.Checked;
}
}
CheckAllFlg = false;
}
You need to define two property as below
public partial class Form1
{
public bool CheckAllFlg { get; set; }
public bool CheckFlg { get; set; }
Upvotes: 5