Ok so here is my problem:I have a form with 10 user control (they are all the same) and they each include 1 textbox, 1 combobox and 5 checkbox.
By default everything is disabled, but with another checkbox the user will enabled either the textbox or the combobox or all 5 checkboxes.
I could easly do it by doing something like
ucPlayer1.name.Enabled = true;
ucPlayer2.name.Enabled = true;
etc .. but it seems unnecessary
Before I wasn't using any usercontrol so I could do something like:
foreach (Control c in this.Controls)
{
if (c is TextBox && c != null)
((TextBox)c).Enabled = true;
}
but now, i'm stuck, I can't get a working loop, i tried something like:
foreach(UserControl uc in Controls)
But it doesn't work.
Any ideas ??
Try something like this. If it is usercontrol then call the same method recursively.
private void DoItRecursive(Control parent)
{
foreach (Control c in parent.Controls)
{
if(c is UserControl)
DoItRecursive(c);
else if (c is TextBox)
c.Enabled = true;
}
}
Then use it like this
DoItRecursive(this);
Where this
refers to Form
typically.
Note: I've removed c != null
checking from your code because is
keyword takes care of that. You don't need to, and casting c
to TextBox
is redundant.