使用Linq获取具有特定属性值的表单控件 [英] Using Linq To Get Form Controls Having A Certain Property Value

查看:44
本文介绍了使用Linq获取具有特定属性值的表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我可以获得具有特定属性值的表单中的控件集合吗?



喜欢:



获取所有带Text =MyText的文本框



我知道我们可以通过遍历表单中的所有控件并通过反射检查属性值来获得此结果。但如果表单有更多控件,那就太昂贵了。还有其他方法吗?

Hi,


Can I Get Collection of controls in a form having a certain property value ?

Like :

Get all Textboxes with Text="MyText"

I know we can get this by looping through all controls in a form and check the property value by reflection. But it is too costly if a form has more controls. Is there any other ways ?

推荐答案

您好,



请试用此代码



Hi ,

Please try this code

Form frm = this; // please set the form you are searching from
IEnumerable<textbox> items = frm.Controls.OfType<textbox>().Where(I => I.Text == "myText");





对于嵌套子项内的搜索它是递归的。





希望这个帮助。



For search within nested childs do it recursively.


Hope this help.


这篇关于使用Linq获取具有特定属性值的表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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