通过部分匹配其名称来查找多个控件

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.

Man*_*tra 5

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)