I have 6 text boxes in 6 lines, 36 in all. Line one, box one is called L1N1, line one, box two is L1N2 etc. I want to dynamically assign values to these text boxes using a string…can this be done in C#? E.g.
private void Generate_Click(object sender, EventArgs e)
{
int[,] numbers = new int[6, 6];
int lin = 0;
while (lin < 6)
{
lin++;
int num = 0;
while (num < 6)
{
num++;
Random random = new Random();
int randomNum = random.Next(1, 45);
"L" + lin + "N" + num /* <--here is my string (L1N1) i want to
set my textbox(L1N1).text to a value
randomNum!*/
I echo Jon Skeet’s sentiments. If you want to be able to locate a control by some row/column system, the easiest way to do so is to put the controls themselves into a collection that allows them to be indexed in a similar way:
Now, if you REALLY wanted to access these textboxes by some string value, you would use the Tag property, which is a general-purpose property of most Controls. You could also name all your textboxes according to some system. Then, a little Linq can get you the textbox you want:
However, understand this will be VERY slow; you’ll be searching the full collection of all controls that exist on the Form every time you want one. Indexing will be much faster.