Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Add a Smart-Tag to a User Control

0.00/5 (No votes)
9 Oct 2008 1  
This is a 3 step sample to include a Smart-Tag in your UserControl

Introduction

This is my first article on this site and I hope it will help somebody. This is a three step sample to get a Smart-Tag in your own UserControl. My intention is to show how to do it, not get into detail in every part of the code.

Background

For this exercise, I made a Textbox control with an extra property to accept numbers only. Please remember that the scope of this code is how to get the smart-tag, not the property on the textbox.

Using the Code

For this exercise, you must be sure to include the references to System.dll, System.Windows.Forms.dll, System.Design.dll and System.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;

OK, let's get started with it:

You will write three classes in the same Usercontrol namespace:

Step 1

Write the TextBox class, including properties and private fields.

You must have typed something like this:

[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
}

Step 2

Now write the Designer for the Textbox and the support for the Smart-tag.  You must have something like this:

[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
}

Step 3

Now write the definition of the smart-tag entries and actions. You must have something like this:

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
}

Any comments are welcome.

History

  • 9th October, 2008: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here