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:
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.
mcDateSelection.TodayDate = Now
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.
Other ways to use the control
There are many other properties and important methods you can utilize to allow the MonthCalendar control to behave the way you need. MSDN offers more details about using the MonthCalendar control.
Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.
————————————————————————————————————————————Get Visual Basic tips in your inbox
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!