We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,993 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Changing multiple controls during runtime

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
17 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
SoulReaper1680
Newbie Poster
22 posts since May 2009
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

This will do it.

for (int I = 1; I <= 4; I++)
            {
                this.Controls["label" + I].Text = "Some text";
            }
WildBamaBoy
Junior Poster
181 posts since May 2010
Reputation Points: 33
Solved Threads: 13
Skill Endorsements: 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 :)

SoulReaper1680
Newbie Poster
22 posts since May 2009
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by WildBamaBoy

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0663 seconds using 2.68MB