use*_*021 4 c# winforms .net-4.5
我目前有 100 多个标签,名称如下:
labelNumber1
labelNumber2
labelNumber3
labelNumber4
....
labelLetter1
labelLetter2
labelLetter3
labelLetter4
....
Run Code Online (Sandbox Code Playgroud)
How would I find all the labels that have "Number" in the controls name? Instead of having to type out labelNumber1.text = "hello", etc.
I have tried regex and foreach with wild cards but did not succeed. I have looked on msdn.microsoft.com about using regex with a control.
You can loop through the Controls collection of the form and just check the name of each control that it contains something like 'Label'. or you could check that the control is a typeof TextBox, Label, etc.
E.g.
foreach (Control control in form.Controls)
{
if (control.Name.ToUpper().Contains("[Your control search string here]"))
{
// Do something here.
}
if (control is TextBox) {
// Do something here.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3757 次 |
| 最近记录: |