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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Adding Smart Tags to Windows Forms Controls : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Servicing the Events
When a user clicks the ComboBox control you need to set the day, month, or year correctly according to the selected format. You accomplish this by servicing the SelectedIndexChanged event for each of the three ComboBox controls (see Listing 1).

You must take special care for cases where the month is represented alphabetically—you need to convert the enumeration value to its corresponding numeric value.

When dealing with dates you must ensure that the date selected by the user is a valid one. For example, February never has more than 29 days, and only 28 days except in leap years. This control checks for date validity every time the ComboBox controls lose their focus. You can use the Date.DaysInMonth() method to find out the number of days in a particular month and year (see Listing 2).

Figure 3. ToolBox: The figure shows the process of adding a new control to the ToolBox.
If the selected date is invalid, the control displays a balloon message (using the ToolTip control).

Testing the Control
That's it! You can now test the control. Right-click the CustomDateControl project item in Solution Explorer and select "Build" to compile the user control into a DLL. After you have compiled the user control, add it to the ToolBox by right-clicking the "All Windows Forms" tab in the ToolBox, and then selecting "Choose Items…" (see Figure 3).

In the Choose Toolbox Items dialog box, click the Browse button and navigate to the bin\Debug folder of the CustomDateControl project. Select the CustomDateControl.dll, click Open, and then click OK. You should now be able to see the DateControl listed in the ToolBox.

Back in the SmartTag Windows application project, drag and drop the DateControl from the ToolBox onto the default Form1 (see Figure 4).

Figure 4. Placing a DateControl: This is how the DateControl looks when you drag and drop it onto the form.
Figure 5. DateControl Properties and Events: These views of the Properties window show the DateControl's properties and events.
Right-click the DateControl on Form1 and select Properties to view its properties. Figure 5 shows the various DateControl properties and events listed under the Date Settings category (you must select the Categorized view in the Properties window to view this).

Figure 6. Populating the Form: Here's a design-time view of the RadioButton controls for the sample form.
To test the control, add a GroupBox control to Form1 and populate it with RadioButton controls as shown in Figure 6. You can see the code for the various RadioButtons in Listing 3.

The code below shows some examples of how you can configure the DateControl user control programmatically.

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---e.g. 1--- With DateControl1 .Format = CustomDateControl.Formats.DDMMYYYY '---display today's date--- .Date = Today End With '---e.g. 2--- With DateControl1 .Format = CustomDateControl.Formats.MmmDDYYYY '---displays Sep 15 1991--- .Date = New Date(1991, 9, 15) End With '---e.g. 3--- With DateControl1 .Format = CustomDateControl.Formats.MMDDYYYY '---displays 12 16 2006--- .Day = 16 .Month = 12 .Year = 2006 End With End Sub

Press F5 to debug the Windows application. Figure 7 shows various views of the DateControl user control, while Figure 8 shows the balloon message the control displays if a user selects an invalid date.

Figure 7. DateControl Views: The figure shows how the RadioButton options affect the display of the DateControl user control.
Figure 8. Balloon Error Message: Here's a how the balloon error message looks if users select an invalid date.

Comment and Contribute






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



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