Reputation: 11184
try to enable or disable some elements on my form (check boxes and textboxes) Read this post , and remake a littlу bit this code
code:
private void checkBoxEnableHotKeys_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxEnableHotKeys.Checked)
{
EnableControls(this.Controls, true);
} //works perfect
if (!checkBoxEnableHotKeys.Checked)
{
EnableControls(this.Controls, false);
} //disable all controls
}
private void EnableControls(Control.ControlCollection controls, bool status)
{
foreach (Control c in controls)
{
c.Enabled = status;
if (c is MenuStrip)
{
c.Enabled = true;
}
if (c.Controls.Count > 0)
{
EnableControls(c.Controls, status);
}
}
checkBoxEnableHotKeys.Enabled = true; //not work
}
where i made mistake? and why checkBoxEnableHotKeys.Enabled = true;
not work? (- during debagging this part of code passing with false - and operation =
not work - false before and false after ...)
Upvotes: 0
Views: 140
Reputation: 8763
I like the methods that return all child controls of a form - including nested controls.
From: Foreach Control in form, how can I do something to all the TextBoxes in my Form?
I like the answer:
The trick here is that Controls is not a List<> or IEnumerable but a ControlCollection.
I recommend using an extension of Control that will return something more..queriyable ;)
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
Then you can do :
foreach(var textbox in this.Controls.All().OfType<TextBox>)
{
// Apply logic to the textbox here
}
Upvotes: 1