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

5 Star
5,164 times
Add to favorites
E-mail Twitter Digg Facebook

Solution explorer

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.ComponentModel.Design

' Custom class to attach designer to
<Designer(GetType(SmartTagButtonDesigner))> _
Public Class SmartTagButton
    Inherits Button

End Class

' Custom control designer that adds the smart tag
Friend Class SmartTagButtonDesigner
    Inherits ControlDesigner

    Dim dalc As DesignerActionListCollection

    Public Overrides ReadOnly Property ActionLists() As DesignerActionListCollection
            ' If this is the first time we're accessing the ActionLists, create one
            If (dalc Is Nothing) Then
                dalc = New DesignerActionListCollection()
                dalc.Add(New SmartTagButtonActionList(Me))
            End If
            Return dalc
        End Get
    End Property

End Class

' 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
Friend Class SmartTagButtonActionList
    Inherits DesignerActionList

    Dim _designer As ControlDesigner
    Dim _showColor As Boolean

    ' 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 Sub New(ByVal _designer As ControlDesigner)
        Me._designer = _designer
    End Sub

    ' Methods need to be void and not take any parameters
    Public Sub Method()
        MessageBox.Show("Method invoked")
    End Sub

    Public Property BackColor() As Color
            ' The property's getter just passes through to the underlying Control
            Return _designer.Control.BackColor
        End Get
        Set(ByVal value As Color)
            ' The setter uses TypeDescriptor to set the property so that change notification is raised
            ' and undo / redo works.
            Dim prop As PropertyDescriptor = TypeDescriptor.GetProperties(Component)("BackColor")
            If (Not prop Is Nothing) Then
                prop.SetValue(Component, value)
            End If
        End Set
    End Property

    ' Boolean properties are shown as checkboxes.
    ' This property toggles whether the method item or the property are 
    ' shown in the smart tag panel
    Public Property ShowColor() As Boolean
            Return _showColor
        End Get
        Set(ByVal value As Boolean)
            _showColor = value
            ' The smart tag panel only refreshes when selection is changed or when 
            ' the Refresh method is called on the DesignerActionUIService.
            Dim uiService As DesignerActionUIService = GetService(GetType(DesignerActionUIService))
        End Set
    End Property

    ' 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 Overrides Function GetSortedActionItems() As DesignerActionItemCollection
        Dim items As DesignerActionItemCollection = 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) Then
            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"))
            items.Add(New DesignerActionPropertyItem("ShowColor", "Show Property", "Configuration", "Toggles whether a method link or a property is shown."))
            items.Add(New DesignerActionMethodItem(Me, "Method", "Show a MessageBox"))
        End If
        Return items
    End Function

End Class