Mark All Checkbox in a groupbox

Asked

Viewed 73 times

2

I have a program that in a groupbox has several checkbox, as there are many and I thought of making a button to select all at once, to be more practical, but there arose a doubt, as I can do when clicking the button automatically select all, and I don’t need to make a CheckBox.Checked = true; for all?

  • It worked, the answer?

3 answers

2


In the search button this GroupBox (in my case is groupBox1) and take your controls which are of the type CheckBox (Cast<>) and make a ForEach setting up your property Checked = true, example:

private void Button1_Click(object sender, EventArgs e)
{
    groupBox1.Controls.Cast<CheckBox>()
        .ToList()
        .ForEach(x => x.Checked = true);
}

0

One more solution if the groupbox does not contain only checkboxs:

groupBox1.Controls.OfType<CheckBox>().All(chk => chk.Checked = true);

0

I usually do this:

foreach (Control ctl in Gbx.Controls)
{
  if (ctl is CheckBox)
  ((CheckBox)ctl).Checked = true;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.