Reputation: 4427
I'm needing to change a bunch of properties in a large amount of controls. I'm having trouble getting it to work. Am I on the right track?
foreach(var c in this.Controls.OfType<Label>())
{
c.Text = "test";
}
What's happening is var c is just creating a new object and not editing the existing one. How can I access the real control?
Upvotes: 1
Views: 2956
Reputation: 13579
you can try this
List<Control> controls = Controls.OfType<Label>().Cast<Control>().ToList();
foreach (Control m in controls)
{
m.Text = "test";
}
Upvotes: 3
Reputation: 13419
Try the following:
foreach(var c in this.Controls)
{
var label = c as Label;
if(label != null) label.Text = "test";
}
Upvotes: 2