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
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 = _
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) _
Dim prop As PropertyDescriptor
prop = _
If prop Is Nothing Then
Throw New ArgumentException( _
"Invalid property.", propName)
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
Set(ByVal value As String)
'---setting the property directly
myOwnButton.Text = value
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.