如何给所有控件添加自定义的属性? 就像tooltip控件,也就是在设计环境中往窗体中添加tooltip控件,该窗体中所有的控件都会增加tooltip属性,设置tooltip的显示内容,这是如何实现的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个的实现其实不难,可以参考下面的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) { }} 整理了一下代码,参考如下: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); } }} datetime获取星期 请问用c#怎样操控ppt。 一个新手的问题!大侠过来帮帮......... 问个正则表达式的问题 一个xml的问题,请思归孟子等大侠来帮忙啦,顶着有分,不够开贴另加 关于网络方面的问题?请高手指教! 求助:关于窗体事件的触发 制作软件的流程是什么 用手机扫描二维码出现公司信息这个功能怎么做 菜鸟想打印form中label标签的内容,请赐教!!! winform 项目怎么编译成dll 痛哭!连不上ORACLE数据库!都快一个月了,哪位帮忙解决下!
这个的实现其实不难,可以参考下面的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)
{
}
}
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);
}
}
}