Putting VB.NET's MonthCalendar control to use

Visual Studio .NET provides the MonthCalendar control, which allows you to select dates or a range of dates. The control is a convenient way to allow users to make a date selection. It is similar to the DateTimePicker control, which only allows you to select a particular date, while the MonthCalendar control also lets you pick a time.

Using the MonthCalendar control

The following example demonstrates how you can utilize the MonthCalendar control.

Find the MonthCalendar control in the Toolbox under the Common Controls section and add it to the Windows Form. Set the following properties:

Name:mcDateSelection

TodayDate:empty

MaxSelectionCount:1

Selection Range Start:empty

Selection Range End:empty

Add the following code to the Form Load event:

mcDateSelection.TodayDate = Now

Then create the event in Listing A. In the example, I set the MaxSelectionCount property to 1, which means that you will only be able to select a specific date in MonthCalendar and not a range of dates. Select a date on the MonthView control, and your screen will look similar to Figure A.

Since you can only select a specific day in the month and not a range of dates, the Selected Start Date and the Selected End Date will have the same value.

Now change the MaxSelectionCount property to 7 and re-run the code. This time you will be able to select a range of dates (up to seven days in this case) and, once you select the range of dates, you will see a result that looks like Figure B.