This is because not all controls in cc are TextBoxes. Therefore, when you try to convert them to a TextBox, the variable is null. When a variable is null, you cannot access any properties of this variable or you will get an error. Therefore, at any time, the variable can be zero, you must first check whether it is zero.
if, , :
if (tb != null && tb.ReadOnly) { tb.ReadOnly = false; }
, , , . , , : 1: GroupBox. -. . , , . - , Controls. , , ( ) , . ( , Controls, , .)
:
private void TextBoxesReadOnlyTrue(Control.ControlCollection cc)
{
foreach (Control ctrl in cc)
{
TextBox tb = ctrl as TextBox;
if (tb != null && tb.ReadOnly)
{ tb.ReadOnly = false; continue; }
if (ctrl.Controls != null && ctrl.Controls.Count > 0)
{ TextBoxesReadOnlyTrue(ctrl.Controls); }
}
}