获取名称以特定字符串开头的所有控件 [英] Get all controls with names that start with specific string
问题描述
对于一个学校项目,我们正在创建一个可供孩子们学习的 C# 应用程序.
For a school project we are creating a C# application where children can learn.
我在windows窗体中做了一个模板,想根据child的选择改变占位符,这样就可以变成1xchoice,2xchoice等
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.
我给每个标签取了一个以 tafel_noemer_
- tafel_noemer_1
、tafel_noemer_2
等开头的名称
I gave every label a name that starts with tafel_noemer_
- tafel_noemer_1
, tafel_noemer_2
, etc.
现在我想选择所有标签直到标签 10 并更改占位符文本.我试过 this.Name.StartsWith("tafel_noemer_")
但不能让它与 foreach 一起工作.
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?
推荐答案
可以使用 Linq 的 where 方法:
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";
}
这篇关于获取名称以特定字符串开头的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!