Putting VB.NET’s MonthCalendar control to use

Started by dhilipkumar, Nov 26, 2008, 03:22 PM

Previous topic - Next topic

dhilipkumar

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.

Listing A

Private Sub mcDateSelection_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mcDateSelection.DateSelected
        MessageBox.Show("Calendar Today's Date: " & mcDateSelection.TodayDate & vbCrLf & _
                " Calendar Selected Start Date: " & mcDateSelection.SelectionStart() & vbCrLf & _
                " Calendar Selected End Date: " & mcDateSelection.SelectionEnd())
    End Sub


Listing 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.

Gotibandhu

Hello,
this is great article thanks for sharing with us.
this article as well as this link http://www.mindstick.com/Articles/1b8d2a50-0957-4e36-9d86-006b9b19bdc7/?MonthCalendar%20Control%20in%20VB.Net
helped me lot in completing my project.

:agreeThanks !!!!!