1,105,594 Community Members

Changing multiple controls during runtime

Member Avatar
SoulReaper1680
Newbie Poster
22 posts since May 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 :)

Member Avatar
WildBamaBoy
Junior Poster
188 posts since May 2010
Reputation Points: 19 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
1
 

This will do it.

for (int I = 1; I <= 4; I++)
            {
                this.Controls["label" + I].Text = "Some text";
            }
Member Avatar
SoulReaper1680
Newbie Poster
22 posts since May 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 :)

Question Answered as of 2 Years Ago by WildBamaBoy
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: