SmartTagSample

The SmartTagSample project shows how to Add a smart tag to a control.

 
 
 
 
 
5 Star
(1)
5,164 times
Add to favorites
2/4/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
VB.NET
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;

namespace SmartTagSample
{
    // Custom class to attach designer to
    [Designer(typeof(SmartTagButtonDesigner))]
    public class SmartTagButton : Button { }

    // Custom control designer that adds the smart tag
    internal class SmartTagButtonDesigner : ControlDesigner
    {
        DesignerActionListCollection dalc;

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                // If this is the first time we're accessing the ActionLists, create one
                if (dalc == null)
                {
                    dalc = new DesignerActionListCollection();
                    dalc.Add(new SmartTagButtonActionList(this));
                }

                return dalc;
            }
        }
    }

    // The DesignerActionList class represents a list of smart tag items (DesignerActionItems)
    // The items can be either properties or methods
    // Override GetSortedActionItems to return the sorted list of smart tag items
    internal class SmartTagButtonActionList : DesignerActionList
    {
        ControlDesigner designer;
        bool showColor;

        // The DesignerActionList base construcotr takes an IComponent.
        // This provides access to the Component the smart tag is attached to 
        // so it can do work on it.  It also allows the action list to provide
        // a GetService implementation
        //
        // It is also common to need access to the Component's deisnger, so it 
        // is passed in here.
        public SmartTagButtonActionList(ControlDesigner designer)
            : base(designer.Component)
        {
            this.designer = designer;
        }

        // Methods need to be void and not take any parameters
        public void Method()
        {
            MessageBox.Show("Method invoked");    
        }

        public Color BackColor
        {
            get
            {
                // The property's getter just passes through to the underlying Control
                return designer.Control.BackColor;
            }
            set
            {
                // The setter uses TypeDescriptor to set the property so that change notification is raised
                // and undo / redo works.
                PropertyDescriptor prop = TypeDescriptor.GetProperties(Component)["BackColor"];
                if (prop != null)
                {
                    prop.SetValue(Component, value);
                }
            }
        }

        // Boolean properties are shown as checkboxes.
        // This property toggles whether the method item or the property are 
        // shown in the smart tag panel
        public bool ShowColor
        {
            get
            {
                return showColor;
            }
            set
            {
                showColor = value;

                // The smart tag panel only refreshes when selection is changed or when 
                // the Refresh method is called on the DesignerActionUIService.
                DesignerActionUIService uiService = GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
                uiService.Refresh(Component);
            }
        }

        // This is the method that returns which smart tag items are displayed at any given time.
        // If this method is not overriden, all properties and methods on the class will be returned
        // in a collection and the sorting will be based on the order items are returned from reflection.
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();

            // Toggle whether the property or the method are shown
            // 
            // Notice that the "ShowColor" item is in the "Configuration" category and the other items are in a different category.
            // The different categories will be seperated by a line in the smart tag panel UI
            // 
            // DesignerActionTextItems and DesignerActionHeaderItems can be used to display text and bolded text respectivly.
            if (showColor)
            {
                items.Add(new DesignerActionPropertyItem("ShowColor", "Show Method Link", "Configuration", "Toggles whether a method link or a property is shown."));
                items.Add(new DesignerActionPropertyItem("BackColor", "BackColor", "Properties", "Modifies the background color of the control"));
            }
            else 
            {
                items.Add(new DesignerActionPropertyItem("ShowColor", "Show Property", "Configuration", "Toggles whether a method link or a property is shown."));
                items.Add(new DesignerActionMethodItem(this, "Method", "Show a MessageBox"));
            }

            return items;
        }
    }
}