Question For Loop to Create an Array of Labels

markBegin2Code

New member
Joined
Mar 2, 2021
Messages
4
Programming Experience
Beginner
Hi
I have a Windows form with some labels on that I want to populate into a Labels array. Putting them into the Array is no problem, but if I have a lot of Labels, or if the number of labels I have changes dynamically I want to populate the labels into an Array with a for loop.

I have created 6 labels on my form (lblNumber1, lblNumber2, lblNumber3, lblNumber4, lblNumber5, lblNumber6)

so I create an Array to work with 6 labels for now
C#:
Label [] lotNumbers = new Label[6];
then I create the for loop
C#:
for(int i = 0; i < 6; i++)
{

    lotNumbers[i] = new Label();
    lotNumbers[i].Name = "lblNumber" + (i + 1);
    this.Controls.Add(lotNumbers);

}
When I run simple Text update, the label does not update?
C#:
lotNumbers[3].Text = "Hello World";
lotNumbers[3].Update();
on the for loop I put a message box
C#:
MessageBox.Show(lotNumbers.Name.ToString());
and I can see the Label Names look like they are going correctly in the Array?

Any ideas?

Thanks
 
Last edited by a moderator:
I am not sure why my original post does not have the square brackets in the loop? Because it does and that is not the issue, but every time I type it in the forum does not show it.

but the for loop does contain in it so that is not the problem
 

Attachments

  • samplecode.txt
    1,023 bytes · Views: 89
I am not sure why my original post does not have the square brackets in the loop? Because it does and that is not the issue, but every time I type it in the forum does not show it.

but the for loop does contain in it so that is not the problem
Given that that is the second post you've made with italics in it for no apparent reason, who knows what you're doing. I just edited your first post to add the brackets without issue. The first and third code snippets both included brackets so I don't see why there would be any issue at all.
 
Your post actually does make sense anyway.
I have a Windows form with some labels on
Do you? If so then why are you creating Labels in code? Are you trying to create and add Labels at run time or are you trying to add existing Labels to an array?
 
In that case, ditch all the code that creates new Labels. Just make sure that all the relevant Labels are in the same container with no other Labels. The container might be the form itself or a Panel or something else. Simply declare your array variable at the class level:
C#:
private Label[] lotNumberLabels;
and then create the array, populate it and assign it to the field all at the same time:
C#:
lotNumberLabels = Controls.OfType<Label>().ToArray();
If you use a container other than the form, reference its Controls collection instead, e.g.
C#:
lotNumberLabels = lotNumberPanel.Controls.OfType<Label>().ToArray();
If you can't isolate the Labels in a container for some reason, you can use something else to differentiate them, e.g. set their Tag property to the same value:
C#:
lotNumberLabels = Controls.OfType<Label>().Where(lbl => ((string) lbl.Tag) == "LotNumber").ToArray();
 
In that case, ditch all the code that creates new Labels. Just make sure that all the relevant Labels are in the same container with no other Labels. The container might be the form itself or a Panel or something else. Simply declare your array variable at the class level:
C#:
private Label[] lotNumberLabels;
and then create the array, populate it and assign it to the field all at the same time:
C#:
lotNumberLabels = Controls.OfType<Label>().ToArray();
If you use a container other than the form, reference its Controls collection instead, e.g.
C#:
lotNumberLabels = lotNumberPanel.Controls.OfType<Label>().ToArray();
If you can't isolate the Labels in a container for some reason, you can use something else to differentiate them, e.g. set their Tag property to the same value:
C#:
lotNumberLabels = Controls.OfType<Label>().Where(lbl => ((string) lbl.Tag) == "LotNumber").ToArray();
Perfect - that is working for me. I like putting them in a container and isolating them. Thanks v much
 
Back
Top Bottom