RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Leverage the Power of the Smart Tag in Your Custom Controls  : Page 3

Learn how to increase your users' productivity by adding Smart Tag support to your custom control.

Creating the Designer Action List
The MyButtonActionList class will create the list of designer action items available in the Smart Tag. Add the MyButtonActionList class to Class1.vb.

Public Class MyButtonActionList
Inherits _

End Class
First, declare the following private member variables:

    '---reference to the custom control
    Private myOwnButton As MyButton
    '---reference to DesignerActionUIService
Private designerActionSvc As _
   DesignerActionUIService = Nothing
Then create the constructor for the class. The constructor accepts a single argument, which is a reference to the custom control.

    Public Sub New(ByVal component As IComponent)
        Me.myOwnButton = component
        Me.designerActionSvc = _
           CType(GetService(GetType( _
           DesignerActionUIService)), _
    End Sub
Next, add a function named GetPropertyByName() to retrieve the various properties of the custom control. I will explain the importance of using this function later on in this article.

Private Function GetPropertyByName( _
   ByVal propName As String) _
       As PropertyDescriptor
        Dim prop As PropertyDescriptor
        prop = _
           TypeDescriptor.GetProperties( _
        If prop Is Nothing Then
            Throw New ArgumentException( _
            "Invalid property.", propName)
            Return prop
        End If
    End Function
Next, declare the various action list items in the Smart Tag as properties. Figure 5 shows the various items in the Smart Tag and their corresponding properties; Listing 1 is the code.

Figure 5. Properties: The screen shot shows the various items in the Smart Tag and their corresponding properties.
Figure 6. Arguments: The screen shot shows the arguments for the Add() method.

Note that for the AutoSize property, setting a value will cause the Smart Tag to refresh. I will hide the Size designer action header if the AutoSize property is set to True, and display it when the AutoSize property is false. This is achieved by overriding the GetSortedActionItems() method (Listing 2).

Specifically, the GetSortedActionItems() method from Listing 2 will populate the Smart Tag with the list of designer action items and arrange them in the order that you specify within this method. Figure 6 shows the arguments for the Add() method of the DesignerActionItemCollection class.

Testing the Application
To test the application, first compile the application by going to Build->Built SmartTag. Then drag and drop the MyButton control from the Toolbox (under the SmartTag Components tab; see Figure 7) onto Form1.

Figure 7. Control Check: Locate the custom control by dragging it to the form.
Figure 8. Checking Your Work: View the Smart Tag to check your work and try setting the properties.

Click on the Smart Tag anchor to view the Smart Tag. Try checking and un-checking the Auto Size checkbox. If it is checked, the Size header will not be visible (see Figure 8). Also, try setting the various properties to prove that the Smart Tag works.

Earlier, I mentioned the need to define the GetPropertyByName() function to retrieve the various properties of the custom control. This is because the properties of a custom control must be set through a proxy property (which, in this case, is returned by the GetPropertyByName() function). You might be tempted to set the property of a control directly, like this:

Figure 9. No Change: Resist the urge to set the properties of the control directly. If you do, the Properties window will not reflect the changes you make.

    Public Property [Text]() As String
            Return myOwnButton.Text
        End Get
        Set(ByVal value As String)
            '---setting the property directly
            myOwnButton.Text = value
        End Set
    End Property
However, in that case, if you were to set, say, the "Text String" item in the Smart Tag to some string such as "OK," the Properties window will not reflect the changes (see Figure 9). Therefore the properties must be set through a proxy.

In this article, you have seen how to create Smart Tags for your custom control. If you are a control developer, adding a Smart Tag will make your control more user-friendly and efficient.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date