Your code is ... um ... odd.
Ignoring the commented out stuff - which will make it not compile so I assume you did it for presentation to us - you have lines that do nothing other than perhaps throw an exception:
Controls.Find(IOName, true).IsReadOnly.ToString();
If it finds the control, it will convert it to "true" or "false" and then discard the string. If it doesn't, it'll throw a null reference exception.
This code:
var textBox = sender as TextBox;
will always result in null since a CheckBox isn't derived from TextBox.
This line will always be false:
if (chkBox.Checked.ToString() == "1")
As will this one:
else if (chkBox.CheckState.ToString() == "0")
Since
bool.ToString
will always return a string
"true"
or
"false"
and never
"0"
or
"1"
YOu have no code to cope with a
if ... else if
that doesn't match either: you need to use
if ... else if ... else
to check.
Your loop does nothing of any use since you don't use
i
inside it, and nothing else changes from iteration to iteration.
Try this:
private void chkboxinfo(object sender, EventArgs e)
{
if (sender is CheckBox cb)
{
textBox.ReadOnly = cb.Checked == CheckState.Checked;
}
}