bwoogie
bwoogie

Reputation: 4427

Changing multiple control properties at once

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

Answers (2)

COLD TOLD
COLD TOLD

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

Kevin Aenmey
Kevin Aenmey

Reputation: 13419

Try the following:

foreach(var c in this.Controls)
{
    var label = c as Label;
    if(label != null) label.Text = "test";
}

Upvotes: 2

Related Questions