如何以编程方式向用户控件添加属性 [英] How to programatically add properties to a user control

查看:102
本文介绍了如何以编程方式向用户控件添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个查询,提示是工具提示如何自动"将工具提示(文本)属性添加到表单上的每个控件.

我进行了搜索,但是除了随意继承和扩展类=/.我想不到如何实现此目标.

有没有一种方法可以仅通过代码将新属性附加到现有控件.

任何帮助将不胜感激,

谢谢George

Hi All,

I have a query which is "how" does a tooltip manage to Add the ToolTip (Text) property to every control on the form automatically.

I have searched but cannot think of how this may be achieved, other than litterally inheriting and extending classes =/.

Is there a way of appending a new property to an existing control purely through code.

Any help would be apreciated,

Thanks George

推荐答案

我想您可以澄清您的问题,并且,如果您在特定的领域分享了自己想要实现的目标,也许可以获得更集中的答复. ,具体示例.您问题的标题:如何以编程方式向用户控件添加属性",可以通过多种方式来解释,但是您问题的内容似乎集中在复制ToolTip的功能上.

如果我只关注您的问题的标题,我的回答可能会提到涉及使用反射的运行时修改或通过继承或接口(设计时)扩展UserControl的技术.

我想(猜测)您在这里寻找的是如何构建扩展提供程序:"控件/组件:在WinForms中,ToolTip是一种奇怪的混合"控件,既是控件又是组件:它实现Extender Provider.

无论如何,请确保并在此处查看此讨论:[ ^ ],然后我会仔细查看OriginalGriff的代码建议.

Microsoft在WinForms ToolTip上的文档含糊不清:您会发现它既作为组件存在,也作为控件记录在文档中.

ToolTip类定义为从Component和IExtenderProvider继承.
I think you can clarify your question, and, perhaps, get more focused responses, if you share what exactly you are trying to achieve here in a specific, concrete example. The title of your question: "How to programatically add properties to a user control," could be interpreted a variety of ways, but the content of your question seems to focus on duplicating a ToolTip''s functionality.

If I focused on just the title of your question, my responses might mention techniques involving run-time modification using Reflection, or extension of UserControls via inheritance or Interfaces (design-time), and more.

I think (guess) what you are looking for here is how to build an "Extender Provider:" Control/Component: in WinForms, the ToolTip is a kind of strange "hybrid" Control that is both a Control, and a Component: it implements Extender Provider.

In any case, be sure and review this discussion here: [^], and I''d look carefully at OriginalGriff''s code suggestions.

Microsoft''s documentation on WinForms ToolTip is ambiguous: you will find it both documented as a Component, which it is, and as a Control.

The ToolTip class is defined as inheriting from both Component, and IExtenderProvider.
public class ToolTip : Component, IExtenderProvider

建议您首先查看以下MSDN资源:[ ^ ]:您会发现许多有用的文章,例如这些旧歌":[

Suggest you start by looking at these MSDN resources: [^], and: [^].

And then do a CP search: start here: [^] : you''ll find many useful articles like these "oldies:" [^], and [^].


这篇关于如何以编程方式向用户控件添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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