Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Article

How to Add a Smart-Tag to a User Control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
9 Oct 2008CPOL1 min read 22.6K   258   17   3
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.

C#
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:

C#
[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:

C#
[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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Praiseit is still good Pin
Southmountain15-May-22 17:08
Southmountain15-May-22 17:08 
GeneralThanks dude. Pin
mrsnipey7-Jul-09 1:22
mrsnipey7-Jul-09 1:22 
General[Message Removed] Pin
hankjmatt13-Oct-08 23:47
hankjmatt13-Oct-08 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.