There is an array of mass numbers. It is necessary to drive it into the textbox components through a loop in order to reduce the size of the code. I do something like this

for (int i = 1; i <= 12; i++) { this.Controls["textbox" + i.ToString()].Text = mass[i-1]; } 

Crashes with the error "Object reference not specified in object instance." What is the problem? I tried to write inside the loop like this:

 cls.frm1.Controls["textbox" + Convert.ToString(i)].Text = mass[i-1]; 

(here cls is a class, and frm1 is a form), but to no avail. The same mistake.

    1 answer 1

    cls.frm1.Controls["textbox" + Convert.ToString(i)] you explicitly return null.

    Do you have a TextBox directly on the form? The Controls property contains only those controls that are directly on the form.

    • Oh, right. TextBox is inside GroupBox. In this case, how can I specify? UPD: No, it's not about GroupBox. After moving the components to bare shape, nothing has changed. The same nonsense. - Jembo_by
    • Is it possible to register? usually default names look like this: textBox1 - Specter
    • No, nothing to do with the register. After several experiments I found out that the code works only if you use this and while the textboxes are on bare form (not in GroupBox) + they must be non-empty. Maybe you know how to point the way to them inside the grouper? - Jembo_by 7:49 pm
    • gruppboxes also have the Controls property, walk on them, or get all group boxes on the ControlCollection and go through all controls inside them - Specter
    • Thank you, it works! - Jembo_by