Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all controls with names that start with specific string

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

like image 292
Debreker Avatar asked Apr 26 '15 12:04

Debreker


1 Answers

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";
}
like image 55
Zohar Peled Avatar answered Oct 29 '22 17:10

Zohar Peled