6

For a school project we are creating a C# application where children can learn.

I made a template in the windows form and want to change the placeholder with the choice of the child, so it can become 1x choice, 2x choice, etc.

I gave every label a name that starts with tafel_noemer_ - tafel_noemer_1, tafel_noemer_2, etc.

Now I want to select all those labels up to label 10 and change the placeholder text. I tried this.Name.StartsWith("tafel_noemer_") but can't get it to work with foreach.

Is there a better way to accomplish this?

my Form

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
Debreker
  • 206
  • 4
  • 12
  • 2
    Possible duplicate of [Find control by name from Windows Forms controls](http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls) and [Find a control in WinForms by name](http://stackoverflow.com/questions/4483912/find-a-control-in-winforms-by-name) – Mehrzad Chehraz Apr 26 '15 at 13:03

1 Answers1

6

You can use Linq's where method:

foreach (Label l in this.Controls.OfType<Label>().Where(l => l.Name.StartsWith("tafel_noemer_")))
{
    l.Text = "bla bla";
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121