Find multiple controls with by partially matching their name

后端 未结 3 1096
别那么骄傲
别那么骄傲 2021-01-19 10:14

I currently have 100+ labels, with names like:

labelNumber1 
labelNumber2 
labelNumber3 
labelNumber4 
....
labelLetter1
labelLetter2 
labelLetter3 
labelLet         


        
3条回答
  •  深忆病人
    2021-01-19 10:52

    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.
        }
    }
    

提交回复
热议问题