Adrian Brown
Adrian Brown

Reputation: 133

Loop through labels and change values c#

I am looking to update some label text from an XML document.

The labels are named supName1, supName2 etc.

I have a for loop which runs through for all the XML nodes in List.Count.

var n = list.Count;
        for (int i = 0; i < n; i++)

I need to update the label text for each of the list.count but I can't figure out how to reference the labels.

From my VBA experience it would be something like "supName"+i but I can't figure it out for C#.

I've tried the following;

var label = (Label)Controls["supName" + i];

but it is returning null when trying to use it as follows;

label.Text = list[i].Attributes["name"].Value;

Upvotes: 1

Views: 2563

Answers (5)

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43946

You need to find the labels in your form by their Name property, but have to keep in mind that they may be placed on a child control, not the form itself. The method that helps you here is ControlCollection.Find() that you can call on your form's Controls property which represents the form's ControlCollection:

int n = list.Count;
for(int i=0; i<n; i++)
{
    // the second argument "true" indicates to 
    // search child controls recursivly
    Label label = Controls.Find($"supName{i}", true).OfType<Label>().FirstOrDefault(); 
    if (label == null) continue; // no such label, add error handling
    label.Text = list[i].Attributes["name"].Value;
}

Upvotes: 1

Shashi Bhushan
Shashi Bhushan

Reputation: 1350

Following code should do atleast for windows form application. thanks

       var labels = this.Controls.OfType<Label>();
            if (labels != null)
            {
                int cnt = 0;
                foreach (var label in labels)
                {
                    label.Text = "New label text " + cnt++;
                }
            }

Upvotes: 1

mrogal.ski
mrogal.ski

Reputation: 5940

These Labels are reference types so you can use one line Linq for that :

Controls.OfType<Label>().Select(lbl => lbl.Text = "hello world!");

If you need to change specific Label then do :

Controls.OfType<Label>().Where(lbl => lbl.Name.EndsWith(index)).Select(lbl => lbl.Text = "hello world!");
// where index -> int index;

So basically to find Label you want use :

Controls.OfType<Label>().Where(lbl => lbl.Name.EndsWith(index));
// or if you want only first matched element 
Controls.OfType<Label>().FirstOrDefault(lbl => lbl.Name.EndsWith(index));

Upvotes: 1

Wheels73
Wheels73

Reputation: 2890

This should do it...

foreach (Label myControl in this.Controls
                 .OfType<Label>()
                 .Where(myControl => (myControl).Name == "ValueFromList"))
            {
                //Apply change here.
            }

Upvotes: 1

Jamiec
Jamiec

Reputation: 136239

The reason this did not work:

var label = (Label)Controls["supName" + i];

Is because controls are hierarchical, and your label is probably not a direct descendant of the current Form/Control.

For this purpose there exists a Find method:

var label = (Label)ControlsCollection.Find("supName" + i,true);

Upvotes: 1

Related Questions