0

Hey,

I have four labels in a form: label1, label2, label3, and label4. Now, if this was VB.NET, I would have used the following code to change their contents without having to type the code for each one separately:

For I = 1 To 4
Me.Controls("label" & I).Text = "Some text"
Next

I was wondering if there is an equivalent method to perform this task in C# as well.

Thank you for your time :)

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by SoulReaper1680
Featured Replies
1

This will do it.

for (int I = 1; I <= 4; I++)
            {
                this.Controls["label" + I].Text = "Some text";
            }
0

This will do it.

for (int I = 1; I <= 4; I++)
            {
                this.Controls["label" + I].Text = "Some text";
            }

Omg, thank you so much! I used ()s instead of []s and it kept giving an error. Once again, thanks :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.