Udostępnij za pośrednictwem


How to: Select a Range of Dates in the Windows Forms MonthCalendar Control

An important feature of the Windows Forms MonthCalendar control is that the user can select a range of dates. This feature is an improvement over the date-selection feature of the DateTimePicker control, which only enables the user to select a single date/time value. You can set a range of dates or get a selection range set by the user by using properties of the MonthCalendar control. The following code example demonstrates how to set a selection range.

To select a range of dates

  1. Create DateTime objects that represent the first and last dates in a range.

    Dim projectStart As Date = New DateTime(2001, 2, 13)
    Dim projectEnd As Date = New DateTime(2001, 2, 28)
    
    DateTime projectStart = new DateTime(2001, 2, 13);
    DateTime projectEnd = new DateTime(2001, 2, 28);
    
    DateTime projectStart = DateTime(2001, 2, 13);
    DateTime projectEnd = DateTime(2001, 2, 28);
    
  2. Set the SelectionRange property.

    MonthCalendar1.SelectionRange = New SelectionRange(projectStart, projectEnd)
    
    monthCalendar1.SelectionRange = new SelectionRange(projectStart, projectEnd);
    
    monthCalendar1->SelectionRange = gcnew
       SelectionRange(projectStart, projectEnd);
    

    –or–

    Set the SelectionStart and SelectionEnd properties.

    MonthCalendar1.SelectionStart = projectStart
    MonthCalendar1.SelectionEnd = projectEnd
    
    monthCalendar1.SelectionStart = projectStart;
    monthCalendar1.SelectionEnd = projectEnd;
    
    monthCalendar1->SelectionStart = projectStart;
    monthCalendar1->SelectionEnd = projectEnd;
    

See Also

Tasks

How to: Change the Windows Forms MonthCalendar Control's Appearance

How to: Display Specific Days in Bold with the Windows Forms MonthCalendar Control

How to: Display More than One Month in the Windows Forms MonthCalendar Control

Other Resources

MonthCalendar Control (Windows Forms)