Click here to Skip to main content
Licence CPOL
First Posted 9 Oct 2008
Views 8,208
Downloads 93
Bookmarked 16 times

How to Add a Smart-Tag to a User Control

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

About the Author

alexlevocitakahashi

Software Developer

Mexico Mexico

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks dude. Pinmembermrsnipey1:22 7 Jul '09  
General[Message Removed] Pinmemberhankjmatt23:47 13 Oct '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Oct 2008
Article Copyright 2008 by alexlevocitakahashi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid