通过部分匹配它们的名称来查找多个控件 [英] Find multiple controls with by partially matching their name

查看:77
本文介绍了通过部分匹配它们的名称来查找多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有100多个标签,名称如下:

I currently have 100+ labels, with names like:

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

我如何找到控件名称中带有数字的所有标签?
不必键入labelNumber1.text = hello,等等。

How would I find all the labels that have "Number" in the controls name? Instead of having to type out labelNumber1.text = "hello", etc.

我尝试使用通配符进行正则表达式和foreach,但未成功。
我在msdn.microsoft.com上查找了有关将正则表达式与控件一起使用的方法。

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.

例如

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

这篇关于通过部分匹配它们的名称来查找多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆