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 2

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

Creating the Project
Using Visual Studio 2005, create a new Windows application. Name the application as C:\SmartTag. All the necessary classes required to build Smart Tag are located in the System.ComponentModel.Design namespace, which is stored in the System.Design.dll assembly. So, you need to add a reference to the System.Design.dll assembly in the project (see Figure 4).
Figure 3. Knowing the Jargon: You'll need to identify some key terms to work effectively with the Smart Tag.
Creating the Component
To create a custom control, add a new Class template to the project and use the default name of Class1.vb. To keep things simple, I am going to simply create a custom Button control that inherits from the standard Button control. And so, in Class1.vb, add the following:

Public Class MyButton
    Inherits System.Windows.Forms.Button
End Class
Note that you can enhance the MyButton control by adding properties and methods. You can also override some of its existing methods and properties.

Creating the Control Designer
The next step is to create the control designer (Smart Tag) for your custom control.

First, add the following namespace at the top of Class1.vb:

Imports System.ComponentModel
Imports System.ComponentModel.Design
Add the following MyButtonDesigner class to Class1.vb:

Public Class MyButtonDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Private lists As DesignerActionListCollection

    Public Overrides ReadOnly Property ActionLists() _
       As DesignerActionListCollection
            If lists Is Nothing Then
                lists = New _
                lists.Add( _
                   New MyButtonActionList(Me.Component))
            End If
            Return lists
        End Get
    End Property

End Class
Figure 4. Adding a Reference: Add the System.Design.dll assembly to the project.
You use the ControlDesigner class for extending the design mode behavior of a Windows control. In this case, you want to display a Smart Tag for the custom control. The MyButtonDesigner class contains a private member variable called lists that contains the Designer Action List. The purpose of this variable is to contain a list of Designer Action Items. This is done by creating an instance of the MyButtonActionList class (which I will define in the next section).

To associate the custom control with the control designer, you need to add the <Designer> attribute to the MyButton class:

<Designer(GetType(MyButtonDesigner))> _
Public Class MyButton
    Inherits System.Windows.Forms.Button
End Class

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date