Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Adding Smart Tags to Windows Forms Controls : Page 4

Microsoft uses smart tags extensively in Office, Visual Studio, and other products; this article shows you how to use smart tags to add value to your controls too.


advertisement
Adding Smart Tags
Now that you have built your user control, you can add a smart tag to it. Adding a smart tag to the control allows developers to configure commonly used properties of the control without having to do so via the Properties window.

The first step is to add a reference to the System.Design.dll assembly to the CustomDateControl project. Right-click the CustomDateControl item in Solution Explorer and select Add Reference…. In the .NET tab, select the System.Design component and click OK.

Next, add the System.ComponentModel.Design namespace to UserControl1.vb using these two Imports statements:

Imports System.ComponentModel Imports System.ComponentModel.Design

Creating the Control Designer
Add a new class definition to the UserControl1.vb file and name the class DateControlDesigner. This class will inherit from the System.Windows.Forms.Design.ControlDesigner class:

Public Class DateControlDesigner Inherits _ System.Windows.Forms. _ Design.ControlDesigner End Class

You use the ControlDesigner class to extend the design-mode behavior of your user controls. In this case, you want to display a smart tag for the user control. Add the following member variables and property:



Public Class DateControlDesigner Inherits _ System.Windows.Forms.Design.ControlDesigner Private lists As _ DesignerActionListCollection Public Overrides ReadOnly _ Property ActionLists() _ As DesignerActionListCollection Get If lists Is Nothing Then lists = New _ DesignerActionListCollection() lists.Add(New DateControlActionList( _ Me.Component)) End If Return lists End Get End Property End Class

The DateControlDesigner class shown above contains a member variable called lists that holds a list of DesignerActionList objects. The DesignerActionList class provides the base class for types that define a list of items used to create a smart tag panel. This variable will contain a list of items that will eventually appear in the smart tag. You do this by creating an instance of the DateControlActionList class. Define the DateControlActionList class as follows, adding the new class definition to UserControl1.vb:

Public Class DateControlActionList Inherits System.ComponentModel. _ Design.DesignerActionList '---indicate whether to display ' today's date--- Private _DisplayTodayDate As Boolean '---reference to the user control Private myDateControl As DateControl '---reference to DesignerActionUIService Private designerActionSvc As _ DesignerActionUIService = Nothing End Class

The DateControlActionList class will create the list of designer action items available in the smart tag. Next, create the class constructor, which accepts a single argument—a reference to the user control:

Public Sub New(ByVal component As _ IComponent) MyBase.New(component) Me.myDateControl = component Me.designerActionSvc = _ CType(GetService(GetType( _ DesignerActionUIService)), _ DesignerActionUIService) End Sub

Add a function named GetPropertyByName() to retrieve the various properties of the user control:

You cannot set the properties of a user control directly; instead, you set them using a proxy property (which, in this case, is returned by the GetPropertyByName() function).

Private Function GetPropertyByName( _ ByVal propName As String) As _ PropertyDescriptor Dim prop As PropertyDescriptor prop = TypeDescriptor. _ GetProperties( _ myDateControl)(propName) If prop Is Nothing Then Throw New ArgumentException( _ "Invalid property.", propName) Else Return prop End If End Function

Next, you need to declare the various action list items in the smart tag as properties (see Listing 4). To display all these properties as items in the smart tag, you will need to add them to the DesignerActionItemCollection object. You accomplish this by using the GetSortedActionItems() function, which you need to override in this class. The GetSortedActionItems() function populates the smart tag with the list of designer action items and arranges them in the order you specify within this function (see Listing 5).

 
Figure 9. Smart Tag Items: The figure shows the association between smart tag items and the various parameters of the DesignerActionPropertyItem class.
Note that setting a value for the DisplayTodayDate property causes the smart tag to refresh; Specifically, you hide the Date designer action item if the DisplayTodayDate property is set to True and display it when the property is False.

Figure 9 shows the various items in the smart tag and the relationships with their corresponding properties.

Finally, to associate the user control with the control designer, you need to add the Designer attribute to the DateControl class:

<DefaultEvent("Err"), Designer(GetType( DateControlDesigner))> _ Public Class DateControl

Viewing the Smart Tag
To view the updated control, you need to rebuild the CustomDateControl project. Right-click the project item in Solution Explorer and select Rebuild. Now, if you examine the DateControl in Form1 (from the SmartTag project), you can click the arrow located on the top-right corner to display the smart tag (see Figure 10).

 
Figure 10. Smart Tag in Action: Here's the completed smart tag displaying the various control properties.
 
Figure 11. Setting the Date: Choosing to display today's date hides the "Set the initial date" item.
Try using the smart tag to set the various control properties. Observe that if you check the "Display today's date" checkbox, the "Set the initial date item" disappears (see Figure 11).

This article showed you how to define and use the various classes that associate a smart tag with a user control. As a control developer, consider incorporating smart tags in your controls to add value and simplify their use.



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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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