I am trying to find multiple labels using a for loop, but I’m only finding the last label. Here is my code:
string input = textBox1.Text;
int n = Convert.ToInt32(input);
for (i = 1; i <= n; i++)
{
label.Name = "label" + i;
label.Location = new Point(100, 100 + i * 30);
label.TabIndex = i;
label.Visible = true;
//label[i].Name=
label.Text = "jjgggg";
this.Controls.Add(label);
}
I want all labels between 1 to 5 if I input 5.
You’re repeatedly assigning to the properties of the same label. You need something like:
You might also want to consider using an object initializer: