如何给所有控件添加自定义的属性?

superzxf 2007-10-12 12:26:35
就像tooltip控件,也就是在设计环境中往窗体中添加tooltip控件,该窗体中所有的控件都会增加tooltip属性,设置tooltip的显示内容,这是如何实现的?
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
superzxf 2007-10-19
  • 打赏
  • 举报
回复
真是nb啊,谢了
长江 2007-10-12
  • 打赏
  • 举报
回复
强悍,我今天就一直在研究这个。真是天外有天了,佩服佩服
北京的雾霾天 2007-10-12
  • 打赏
  • 举报
回复
整理了一下代码,参考如下:


using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;

namespace ExtenderApp
{
[ProvideProperty("TT", typeof(Control))]
class testTT : Component, IExtenderProvider
{
private Dictionary<Control, string> m_valueList;
public testTT()
{
m_valueList = new Dictionary<Control, string>();
}
public bool CanExtend(object target)
{
return ((target is Control) && !(target is testTT));
}

[DefaultValue(""), Localizable(true), Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string GetTT(Control control)
{
if (control == null)
{
return string.Empty;
}
if (this.m_valueList.ContainsKey(control))
{
return this.m_valueList[control];
}
return "";
}
public void SetTT(Control control, string caption)
{
this.m_valueList.Add(control, caption);
}
}
}
北京的雾霾天 2007-10-12
  • 打赏
  • 举报
回复

这个的实现其实不难,可以参考下面的TT这个类的实现。
主要是三点做到就可以了:
1:为要实现的类添加ProvidePropertyAttribute属性
2:类要继承于IExtenderProvider接口并实现其接口成员
3:要实现ProvidePropertyAttribute的PropertyName的Get及Set方法

参考实现类如下:

[ProvideProperty("TT", typeof(Control))]
class tt : Component, IExtenderProvider
{
public bool CanExtend(object target)
{
return ((target is Control) && !(target is tt));
}

[DefaultValue(""), Localizable(true), Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string GetTT(Control control)
{
if (control == null)
{
return string.Empty;
}
return "测试测试";
}
public void SetTT(Control control, string caption)
{

}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧