1

So I am making a "Disable all checked checkboxes" button for my windows form application in c#. The code I have works fine when set to loop through a specific panel, like so:

private void LockChecked_Click(object sender, EventArgs e)
    {
        foreach (Control c in block1Panel.Controls)
        {
            if (c is CheckBox)
            {
                CheckBox cb = (CheckBox)c;
                if (cb.Checked == true)
                {
                    cb.Enabled = false;
                }
            }
        }
    }

But what I'd like to do is loop through all the block panels (block1Panel, block2Panel, block3Panel, etc.) that are inside a main panel (Assignments_Panel).

So, how can I iterate through all the checkboxes from all panels, without having to write a loop for each panel? I know it's possible, but since I'm only a beginner I'm not able to figure this one out, even after hours of searching...

Thank you in advance! And if anything in my question is unclear please say so, so I can explain further!

avecrem
  • 21
  • 5
  • 1
    You add a foreach loop on the main panel's block panels. The rest of your code remains the same, except `block1Panel` is replaced by the element in the foreach – Chronicle Feb 21 '20 at 14:10
  • 1
    https://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – Dmitry Bychenko Feb 21 '20 at 14:11
  • thanks, @Chronicle ! I couldn't figure out how to write that first loop but thanks to this and Danny's answer got it to work :) – avecrem Feb 21 '20 at 14:24

2 Answers2

1

This method may help. It loops through each control from a parent control, which in your case looks as tough it'd be Assignments_Panel, then for each control that belongs to the parent control, it will either loop through all child controls again, or disable the control, if it is a checkbox.

private void DisableCheckboxes(Control parentControl)
{
    foreach (Control childControl in parentControl.Controls)
    {
        if (childControl is Panel childPanel)
        {
            DisableCheckboxes(childPanel);
        }
        else if (childControl is CheckBox childCheckBox)
        {
            childCheckBox.Enabled = false;
        }
    }
}
Danny Goodall
  • 798
  • 1
  • 9
  • 33
  • Thanks! With this and Chronicle's comment I got it to work! I just had to add the `foreach (Control childControl in parentControl.Controls)` before my code and with the rest untouched worked like a charm :) Now, how do I give someone reputation? – avecrem Feb 21 '20 at 14:23
  • @mralan1732 happy to help. Clicking the arrow to the left of an answer that points upwards indicates that an answer is useful. And it looks like you've already figured out how to accept an answer :) – Danny Goodall Feb 21 '20 at 14:26
0

Well, we can enumerate all the controls within form while checking if the control is of type CheckBox; there are many implmentations for this, say

How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

let's write good old Bread First Search (no Linq, recursion etc.)

 private void LockChecked_Click(object sender, EventArgs e) {
   Queue<Control> agenda = new Queue<Control>(new [] { this });

   while (agenda.Count > 0) {
     Control control = agenda.Dequeue();

     if (control is CheckBox cb && cb.Checked)
       cb.Enabled = false;

     foreach (Control child in control.Controls)
       agenda.Enqueue(child);
   }
 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215