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

Posted on 2007-10-12
Last Modified: 2008-01-09
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?

Question by:Karl66
    LVL 96

    Expert Comment

    by:Bob Learned
    Missing information:
    1) .NET framework version (2.0, 1.1, ...)
    2) Application type (WinForms, ASP.NET, ...)

    LVL 5

    Accepted Solution

    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];
    LVL 5

    Assisted Solution

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now