954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with label array

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?

lt johnson
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

lt johnson
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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]);
}
predator5047
Newbie Poster
14 posts since Dec 2011
Reputation Points: 10
Solved Threads: 5
 

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

kamilacbe
Junior Poster in Training
77 posts since Jun 2010
Reputation Points: 10
Solved Threads: 10
 

Thanks! that fixed the problem

lt johnson
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: