65.9K
CodeProject 正在变化。 阅读更多。
Home

如何向用户控件添加智能标签

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (6投票s)

2008年10月9日

CPOL
viewsIcon

23081

downloadIcon

270

这是一个包含 3 个步骤的示例,用于在您的 UserControl 中包含一个 Smart-Tag。

引言

这是我在本网站上的第一篇文章,希望对大家有所帮助。这是一个获取用户控件中智能标签的三步示例。我的意图是展示如何操作,而不是深入探讨代码的每个部分。

背景

为了这个练习,我创建了一个Textbox控件,并添加了一个额外的属性来仅接受数字。请记住,这段代码的范围是如何获取智能标签,而不是textbox上的属性。

Using the Code

为了这个练习,您必须确保包含对System.dllSystem.Windows.Forms.dllSystem.Design.dllSystem.Drawing.dll的引用。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Collections;
using System.Reflection;

好的,让我们开始吧

您将在同一个Usercontrol命名空间中编写三个类

步骤 1

编写TextBox类,包括属性和private字段。

您应该输入类似以下内容

[Designer(typeof(TxTNumCocDesigner))]
public partial class TxTNumCoc : System.Windows.Forms.TextBox
{
#region Campos de la Clase
    private bool Numbered = false;
#endregion
#region Constructores de la Clase
    public TxTNumCoc()
    {
        InitializeComponent();
    }
#endregion
#region Propiedades de la Clase
    [Category("Special properties")]
    [Description("Limits to only numbers capture")]
    public bool SoloNumeros
    {
        get  {return this.Numbered;}
        set{this.Numbered = value;}
    }
#endregion
#region Eventos de la Clase
    private void TxTNumCoc_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (this.Numbered == true)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
            if (Char.IsPunctuation(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
#endregion
}

第二步

现在编写Textbox的设计器和智能标签的支持。您应该有类似以下内容

[System.Security.Permissions.PermissionSet(
    System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class TxTNumCocDesigner : System.Windows.Forms.Design.ControlDesigner
{
#region Campos de la Clase
    private DesignerActionListCollection actionList;
#endregion
#region Propiedades de la Clase
    public override DesignerActionListCollection ActionLists
    {
        get
        {
            if (null == actionList)
            {
                actionList = new DesignerActionListCollection();
                actionList.Add(new TxTNumCocActionList(this.Component));
            }
            return actionList;
        }
    }
#endregion
}

步骤 3

现在编写智能标签条目和操作的定义。您应该有类似以下内容

public class TxTNumCocActionList : System.ComponentModel.Design.DesignerActionList
{
#region Fields of the Class
    private TxTNumCoc txtCoc;
    private DesignerActionUIService designerActionUISvc = null;
#endregion
#region Constructors of the Class
    public TxTNumCocActionList(IComponent component)
        : base(component)
    {
        this.txtCoc = component as TxTNumCoc;
        this.designerActionUISvc =
            GetService(typeof(DesignerActionUIService))
            as DesignerActionUIService;
    }
#endregion
#region Properties of the Class
    public bool SoloNumeros
    {
        get{return txtCoc.SoloNumeros;}
        set
        {
            GetPropertyByName("SoloNumeros").SetValue(txtCoc, value);
            this.designerActionUISvc.Refresh(this.Component);
        }
    }
#endregion
#region Method of the class
    private PropertyDescriptor GetPropertyByName(string propName)

    {
        PropertyDescriptor prop;
        prop = TypeDescriptor.GetProperties(txtCoc)[propName];
        if (null == prop)
            throw new ArgumentException(
            "Property not found!",
            propName);
        else
            return prop;
    }
    public override DesignerActionItemCollection GetSortedActionItems()
    {
        DesignerActionItemCollection items = new DesignerActionItemCollection();
        items.Add(new DesignerActionHeaderItem("Apariencia"));
        items.Add(new DesignerActionHeaderItem("Información"));
        items.Add(new DesignerActionPropertyItem
	("SoloNumeros", "Sólo Números", "Apariencia", 
	"Limita el uso del TextBox a solo números"));
        StringBuilder location = new StringBuilder("Location: ");
        location.Append(txtCoc.Location);
        StringBuilder size = new StringBuilder("Size: ");
        size.Append(txtCoc.Size);
        items.Add(new DesignerActionTextItem(location.ToString(), "Información"));
        items.Add(new DesignerActionTextItem(size.ToString(), "Información"));
        return items;
    }
#endregion
#region Events of the class
#endregion
}

欢迎提出任何意见。

历史

  • 2008年10月9日:初始发布
© . All rights reserved.