就像tooltip控件,也就是在设计环境中往窗体中添加tooltip控件,该窗体中所有的控件都会增加tooltip属性,设置tooltip的显示内容,这是如何实现的?

解决方案 »

  1.   


    这个的实现其实不难,可以参考下面的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)
    {

    }
    }
      

  2.   

    整理了一下代码,参考如下:
    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);
    }
    }
    }