0

I have wrote some code to automatically make labels. Now I want to call each label in another void. I gave all the labels a label.Name, but I can't call them with that. How can I call each label? For example I want this in another void:

label12.BackColor = Color.Blue;
void Test()
{
for (j = 0; j < 20; j++)
{
    label = new Label();
    label.AutoSize = false;
    label.Name = "label" + j.ToString() + i.ToString();
    label.TabIndex = 0;
    label.Text = "[" + j.ToString() + "," + i.ToString() + "]";
    label.Visible = true;
    this.Controls.Add(label);
    label.Location = new Point(Size.Width / 2 - (5 - i) * 50, Size.Height / 2 - (11 - j) * 30);
    label.Size = new Size(50, 30);
    label.BackColor = Color.White;
    if (i % 2 == 0)
    {
        label.BackColor = Color.Blue;
    }
}
}
  • Don't use the linked duplicate. Just make a class member variable which is an array (for your case where things are found by numeric index) or a dictionary (for things which should be found by name), and store your Label handle into that array/dictionary after you create the `Label` object. That would look something like `all_labels[i][j] = label;` near the end of your loop. – Ben Voigt May 11 '22 at 19:20
  • What type must be before the all_labels? Because it hasn't been created before – MajorAnonymous May 11 '22 at 19:25
  • 1
    `Label[][] all_labels;`. or `List – Ben Voigt May 11 '22 at 19:29

0 Answers0