Hello,

I need a label array for my windows form so I can easily edit the text in the labels.
I've found this solution:

for (int i = 0; i < 10; i++)
            {
                Label[] labels = new Label[10];
                labels[i] = new Label();
                labels[i].Text = "0";
                labels[i].Location = new System.Drawing.Point(100, i*10+100);
            }

I put this code in the form load.
It's not giving me errors but It's not displaying the labels on the screen either.

Can anyone help me?

Recommended Answers

Where is the code that tells the form it has new labels?

Jump to Post

You forgot to add the control to the forum,you add controls with the instruction this.Controls.Add(control here).
This is the corect code :

Label[] labels = new Label[10];
for (int i = 0; i < 10; i++)
{
     labels[i] = new Label();
     labels[i].Text = "0";
     labels[i].Location = new …
Jump to Post

All 5 Replies

Where is the code that tells the form it has new labels?

I didn't any other code except the code shown here so should there be code that tells the form it has new labels?

You forgot to add the control to the forum,you add controls with the instruction this.Controls.Add(control here).
This is the corect code :

Label[] labels = new Label[10];
for (int i = 0; i < 10; i++)
{
     labels[i] = new Label();
     labels[i].Text = "0";
     labels[i].Location = new System.Drawing.Point(100, i * 10 + 100);
     this.Controls.Add(labels[i]);
}

Add the control to your form using the method
"this.controls.add(yourcontrol_obj_name)"

Thanks! that fixed the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.