9
Reply

C# WinForms Arrays of Textboxes etc.

Tj

Tj

Mar 31 2010 8:20 AM
6.7k
Is there a simple, programmatic way of getting a reference to an object via the name of the object?

For example, if one created a grid of, say, 6 Textboxes in the Design Window, just wondering if there is a better way simple programmatic way of assigning an array of references to the textboxes?

i.e. a better way than hard-coding like this:

            TextBox[] tb = new TextBox[6];
       tb[0] = textBox1;
       tb[1] = textBox2;
       tb[2] = textBox3;
       tb[3] = textBox4;
       tb[4] = textBox5;
       tb[5] = textBox6;


Could one create a for loop with a string of the textbox's name, i.e. string.Format("textBox{0}",i+1)  ?
But how can one assign that particular textbox by its name?


Answers (9)