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

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

问题描述

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

I currently have 100+ labels, with names like:

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

如何找到控件名称中带有Number"的所有标签?而不必输入 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.

推荐答案

您可以循环访问表单的 Controls 集合,只需检查每个控件的名称是否包含诸如标签"之类的内容.或者您可以检查该控件是否为 TextBox、Label 等类型

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天全站免登陆