如何收集特定控件 [英] How collect specific controls

查看:85
本文介绍了如何收集特定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


为了记录我的项目,我需要收集一个特定控件的列表,例如表单上的所有按钮都是工具提示文本,假设button1用于保存数据,它是工具提示文本是"发布到数据库" ,我如何收集所有控件作为
数据源并绑定它组合框并设置提到的数据作为显示和值配置是否可能?



谢谢




Esmat

解决方案

我不是真的确定如何记录您的项目和UI的布局,以便我猜测您的需求。在表单的运行时,您想知道它上面的按钮吗?这可以使用Controls属性来完成。 

 var buttons = Controls.OfType< Button>(); 

给定一个按钮,您可以访问所有属性。请注意,此代码忽略任何嵌套控件(面板,用户控件等)。如果您需要子控件中的按钮,那么您还必须抓取任何容器
的子控件并枚举它们。


至于绑定到组合,这与绑定任何数据源没有什么不同,因此您可以简单地使用DataSource属性。请参阅

MSDN
,了解如何在Winforms中绑定数据。


如果您改为需要以编程方式对源代码执行此操作,然后您将遇到更困难的时间。更好的方法是可能正常编译程序集,然后使用反射来查找您关心的表单类型。然后你可以
枚举寻找Button字段的字段。但是这不会允许你在运行时挂起任何东西,除非你事先存储了信息或者你在运行时(你的应用程序)使用了反射来做它。



Hi all,

In order to document my projects i need to collect a list of specific control for example all buttons on a form with it is tool tip text, assume button1 for saving data and it is tool tip text is "Post to database" ,how i collect all controls as datasource and bind it combobox and set mentioned data as display and value configuration is it possible?

Thanks


Esmat

解决方案

I'm not really sure how documenting your project and the layout of the UI relate so I'm guessing at your needs. At runtime of your form you want to know the buttons on it? That can be done using the Controls property. 

var buttons = Controls.OfType<Button>();

Given a button you have access to all the properties. Note that this code is ignoring any nested controls (panels, user controls, etc). If you need buttons in child controls then you have to also grab any child controls that are containers and enumerate them as well.

As for binding to a combo, this is no different than binding any data source so you can simply use the DataSource property. Refer to MSDN for how to bind data in Winforms.

If you instead need to do this programmatically against the source code then you're in for a far harder time. The better route would be to probably compile the assembly normally and then use reflection to find the form type(s) you care about. Then you can enumerate the fields looking for Button fields. But this isn't going to allow you to hook anything up at runtime unless you either stored the info in advance or you used reflection at runtime (of your app) to do it.


这篇关于如何收集特定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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