Does anyone have a piece of code to make all Controls (or even all TextBoxes) in a Form which is read-only at once without having to set every Control to read-only individua
Write an extension method which gathers controls and child controls of specified type:
public static IEnumerable GetChildControls(this Control control) where T : Control
{
var children = control.Controls.OfType();
return children.SelectMany(c => GetChildControls(c)).Concat(children);
}
Gather TextBoxes on the form (use TextBoxBase
to affect RichTextBox
, etc - @Timwi's solution):
IEnumerable textBoxes = this.GetChildControls();
Iterate thru collection and set read-only:
private void AreTextBoxesReadOnly(IEnumerable textBoxes, bool value)
{
foreach (TextBoxBase tb in textBoxes) tb.ReadOnly = value;
}
If want - use caching - @igor's solution