0

I am trying to write a code which will uncheck all checkBoxes in my form when a button is clicked. I know that I could do

checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;

and so on, but I have about 35 checkBoxes, so I was hoping to use a simpler code. I keep finding stuff online that looks like this;

foreach (Control cBox in this.Controls)
{
    if (cBox is CheckBox)
    {
        ((CheckBox)cBox).Checked = false;
    }
}

And I was hoping to use something like that, instead of writing checkBox1.Checked = false; about 70 times (because I have 2 buttons, both of which need to do various things and then uncheck all the boxes.

I saw another solution that involved a Stack Panel or some type of code that looked more like Javascript or HTML than C#, and also seemed to involved writing out each checkBox.Checked status as its own line, which doesn't save me any lines of code.

Any tips would be appreciated. Thanks all :)

TnD_Guy
  • 113
  • 1
  • 2
  • 13
  • 4
    The code you posted looks fine. Why don't you try that? – heijp06 Dec 18 '18 at 19:19
  • @PetervanderHeijden I did and it doesn't work. The boxes all are still checked after clicking the button. – TnD_Guy Dec 18 '18 at 19:20
  • 2
    Are your check boxes within another container such as a groupbox? – UnhandledExcepSean Dec 18 '18 at 19:22
  • @UnhandledExcepSean I don't think so. I'm new to this c# stuff. But as far as I know, they are all just independent checkBoxes on the form. – TnD_Guy Dec 18 '18 at 19:23
  • Look at https://stackoverflow.com/a/46449296/1980202 – Tiago Crizanto Dec 18 '18 at 19:23
  • @TiagoCrizanto That works for chkListBox (a listBox with several checkBoxes in it), but what I have is several independent checkBoxes. I have a chkListBox also and I use similar code to handle that one. – TnD_Guy Dec 18 '18 at 19:25
  • Basically you have the right approach. But this answer might be helpful if your controls are nested to recursively visit all controls instead of just the direct children.. https://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – Wyck Dec 18 '18 at 19:25
  • i have added 3 checkboxes directly to my form and copy-pasted your code. it works great on button click. are you sure that your controls aren't placed on any panel or other grouping component like @UnhandledExcepSean mentioned? – Maciej S. Dec 18 '18 at 19:27
  • A `StackPanel` is WPF. The closest thing in Winforms is a `FlowLayoutPanel`. – Handbag Crab Dec 18 '18 at 19:30
  • @Maciej I think that @Wyck has nailed my issue. I suspect that the code doesn't know about the checkBoxes, but honestly that makes no sense to me because I have a button click which does a buncha stuff involving the checkBoxes and then at the very end, it's supposed to uncheck them. It's doing everything else so I don't think it's a dependency or access restriction issue.The checkBoxes are in a Tab on the form, but again, everything else works. Even the `checkBox1.Checked = false;` works just fine. – TnD_Guy Dec 18 '18 at 19:32
  • In Visual Studio, open the designer view of your form then open the View menu and then open the Other Windows sub-menu, finally select `Document Outline`. That window will show you the parentage of your CheckBoxes. – Handbag Crab Dec 18 '18 at 19:33
  • `The checkBoxes are in a Tab on the form`. There's your issue. `this.Controls` only looks at the control your code is in, in your case the Form. So it will only look at the controls added to the form itself, not the controls added to sub-controls. Instead of `this.Controls` substitute it for `mytabname.Controls` where mytabname is the name of the instance of the tab that contains the checkboxes. – Handbag Crab Dec 18 '18 at 19:35
  • @HandbagCrab BAM that was it. I wonder why I don't have to specify `tabPage1` for the `checkboxes` for ANYTHING else that I do? Like, I can read their status and return things based on them in the same curly brackets as the code that was giving me trouble. Well, either way, thank you!!! I'm just starting out with this programming stuff and this website has been a god send. Most of my questions have already been asked before! – TnD_Guy Dec 18 '18 at 19:43
  • That's because you're accessing the checkboxes by their instance when you interact with them at other times. An apt analogy would be that you have their telephone number so can contact them directly, but in the loop you don't have their telephone number so have to ring their buildings reception to get them to put you through. – Handbag Crab Dec 18 '18 at 19:46

2 Answers2

3

Answering my own question with some info from my new pal @HandbagCrab:

foreach (Control cBox in tabPage1.Controls)
{
if (cBox is CheckBox)
    {
        ((CheckBox)cBox).Checked = false;
    }
}

Adding the tabPage1 fixed my issue. Before, I had been using this.Controls which limited access to controls only within that dependency. I was still able to control other things based on the checkBoxes by naming them individually like checkBox1.Checked = false;, but this was only because I called to them by name, rather than asking the code to look through all Controls.

TnD_Guy
  • 113
  • 1
  • 2
  • 13
1

You can use C# 7.0+ pattern matching:

foreach(Control control in controls)
{
    if (control is CheckBox chk) chk.Checked = false;
}
JohnyL
  • 6,894
  • 3
  • 22
  • 41