I am looking to update some label text from an XML document.
The labels are named
supName1
supName2
var n = list.Count;
for (int i = 0; i < n; i++)
"supName"+i
var label = (Label)Controls["supName" + i];
label.Text = list[i].Attributes["name"].Value;
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;
}