Adding Smart Tags to Windows Forms Controls
One new features in Visual Studio 2005 is the support for smart tags. A smart tag is a panel that displays next to a control and contains a list of commonly used properties.
The DataGridView control in Windows Forms has a smart tag (with the header “DataGridView Tasks”) showing a list of commonly used properties (Figure 1). In .NET 2.0, developers can add the smart tag feature to their custom/user controls. In this article, I will walk you through a real world project that I have done that shows how to add your own smart tags to controls.
Figure 1: Examining the smart tag of the DataGridView control.
You can use the DaysInMonth() method from the Date class to find out the number of days in a particular month and year.
I’ll show you how to create a custom date control. My client specifically wanted an easy way to enter dates without using the mouse to select a date. In addition, they wanted the ability to customize the format in which the date is displayed-either numerically or alphabetically-to represent the month. On the surface this task looks easy to solve-simply add a couple of TextBox controls to the form and configure them to display day, month, and year accordingly. However, the client had numerous forms requiring date entry, and thus I decided that a better solution was to create a user control that aggregates all the TextBox controls into one single control. In the first part of this article I’ll show you how to create the user control and in the second part of the article I’ll show you how to add smart tag support to the user control.
Developing the User Control
Using Visual Studio 2005, create a Windows Application project named SmartTag. This Windows application will act as a host for the user control that I’ll show you how to build in this article.
Now, add a new Windows Control Library project to the existing solution. In Solution Explorer, right-click the SmartTag solution and select Add > New Project…. Select the Windows Control Library template and name the project CustomDateControl. Click OK. The left of Figure 2 shows the default design surface of the user control (default name is UserControl1.vb). Populate the design surface with three ComboBox controls shown on the right of Figure 2.
Figure 2: The design surface of the user control and the controls added to it.
Set the properties of the three ComboBox controls with the values shown in Table 1. The first two ComboBox controls will display either the day or month (depending on the user’s configuration) while the third ComboBox control will display the year. Users can either click the ComboBox control to select a date or simply type the date using the keyboard.
Programming the User Control
With the design of the user control completed, you can now start the coding. Switch to the code behind of UserControl1.vb and add the following namespace:Imports System.ComponentModel
The System.ComponentModel namespace provides classes that are used to implement the run-time and design-time behavior of components and controls. In this case, I will be adding attributes to my control class and its properties to customize its behavior. You will see its uses shortly.
Next, declare the Formats and Months enumeration to represent the format and months, respectively:Public Enum Formats
DDMMYYYY '---e.g. 29 02 2006---
DDMmmYYYY '---e.g. 12 Jan 2006---
MMDDYYYY '---e.g. 02 25 2006---
MmmDDYYYY '---e.g. Jan 15 2006---
Public Enum Months
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12