Share via


How to: Select a Range of Dates in a Calendar Control

This example selects a range of dates in a Windows Forms MonthCalendar control. In this example, when the user selects a date, the week is selected. You can use this code to select a range of dates in a week by changing the parameter of the AddDays method.

Example

private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
    DateTime startDate = e.Start;
    startDate = startDate.AddDays(-(double)startDate.DayOfWeek);
    monthCalendar1.SelectionStart = startDate;
    monthCalendar1.SelectionEnd = startDate.AddDays(6);
}

Compiling the Code

This example requires:

  • A Windows Form with a MonthCalendar control named monthCalendar1. Set the DateSelected event handler of monthCalendar1 to monthCalendar1_DateSelected.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Date and Time Controls

Visual C# Guided Tour