Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1597
  • Last Modified:

C# How can loop through a series of labels to update the labels text

I am getting some great help from some great C# minds! Here is another question.

I am cycling through a loop six times to update six labesl (i.e. label1, label2, label3.. etc) rather than label1.Text and label2.Text etc I want to loop through them so I tried:

 label + Convert.ToString(intCounter).Text = newLabel;

It really doesn't like that. Any thoughts?

Karl66
0
Karl66
Asked:
Karl66
2 Solutions
 
Bob LearnedCommented:
Missing information:
1) .NET framework version (2.0, 1.1, ...)
2) Application type (WinForms, ASP.NET, ...)

Bob
0
 
gocemiCommented:
Try this:
for(int i=0; i < x; i++)
{
Control c = this.FindControl("label "+i);
if(c!= null and c is TextBox )
(c as TextBox).Text = [Value];
}
0
 
fmonroyCommented:
A quick and dirty way is:

foreach(Control Ctrl in Controls) {
      if(Ctrl.Name == "label1")
            ((Label)Ctrl).Text = "NEWTEXT";
}

just add ORs in the if or any logic you need.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now