I have two form (Form1,form2)
this code on form2 …
I make a loop of all controls on form1 and get name of the control (ControlName)
I want send any text (ex.”sherif”) to this control (case button)
foreach (Control ctrl in form1.Controls)
{
form1.Controls[ControlName]).Text = "sherif";
}
error message appears
NullReferenceException
Object reference not set to an instance of an object.
if the pointer stop above [ControlName] read name of control, but when continue
Comes an Null value
If you just want to change the
Textproperties of all controls in a form, this should do it:Note this will only change the controls in the top level and not nested controls….
If you need this for nested controls too, do it recursively: