智能标签,提供快捷的控件属性编辑。毋庸多说,见下图1:


给自定义Control加上Smart Tag
 

如何在自定义的控件里也添加上SmartTag呢?本文作简单描述。

秘密就在自定义控件的Designer里面。

ControlDesigner. ActionLists 属性用以实现具体的SmartTag

首先需要写一个从DesignerActionList派生的类用来实现我们的SmartTag,本文只在SmartTag上放了一个TextBox


给自定义Control加上Smart Tagpublic class CustomControlActionList :System.ComponentModel.Design.DesignerActionList

 

看到我们加了一个DesignerActionPropertyItem用以编辑Text

public DesignerActionPropertyItem(

               string memberName,

               string displayName,

               string category,

               string description

)

参数memberName,变量名

参数displayName,显示的名称

参数category,类别名称

参数description,描述,在ToolTip中显示

注意到我给它的Category传的是Appearance,为了好看我想给它加个类别,怎么加呢?如下:

item.Add(new DesignerActionHeaderItem("Appearance"));

显然Text是个Property,我需要告诉SmartTag怎么得到Text的值,怎么把编辑的值送回去,在CustomControlActionList类中创建一个“Text”属性就可以了


给自定义Control加上Smart Tagpublic string Text

 

设值是通过反射去做的。

 

好了,接下来我们要做的事情就是把它添加到ControlDesigner中了

给自定义Control加上Smart Tagpublic class CustomControlDesigner : ControlDesigner

 

很简单吧,好了看看我们的CustomControl效果:
给自定义Control加上Smart Tag
 

给出所有的代码:

 

CustomControl1


给自定义Control加上Smart Tagusing System;
给自定义Control加上Smart Tag
using System.Collections.Generic;
给自定义Control加上Smart Tag
using System.ComponentModel;
给自定义Control加上Smart Tag
using System.Data;
给自定义Control加上Smart Tag
using System.Drawing;
给自定义Control加上Smart Tag
using System.Text;
给自定义Control加上Smart Tag
using System.Windows.Forms;
给自定义Control加上Smart Tag
给自定义Control加上Smart Tag
namespace WindowsApplication1

 

CustomControlDesignerCustomControlActionList


给自定义Control加上Smart Tagusing System;
给自定义Control加上Smart Tag
using System.Collections.Generic;
给自定义Control加上Smart Tag
using System.Text;
给自定义Control加上Smart Tag
using System.Windows.Forms.Design;
给自定义Control加上Smart Tag
using System.Windows.Forms.Design.Behavior;
给自定义Control加上Smart Tag
using System.ComponentModel.Design;
给自定义Control加上Smart Tag
using System.ComponentModel;
给自定义Control加上Smart Tag
给自定义Control加上Smart Tag
namespace WindowsApplication1

相关文章:

  • 2021-10-20
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2021-05-17
猜你喜欢
  • 2021-12-20
  • 2021-07-22
  • 2022-12-23
  • 2021-08-19
  • 2021-09-24
  • 2021-10-26
相关资源
相似解决方案