This documentation is archived and is not being maintained.

MonthCalendar.SelectionStart Property

Gets or sets the start date of the selected range of dates.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public DateTime SelectionStart { get; set; }
/** @property */
public DateTime get_SelectionStart ()

/** @property */
public void set_SelectionStart (DateTime value)

public function get SelectionStart () : DateTime

public function set SelectionStart (value : DateTime)

Not applicable.

Property Value

A DateTime indicating the first date in the selection range.

Exception typeCondition

ArgumentOutOfRangeException

The date value is less than MinDate.

-or-

The date value is greater than MaxDate.

If you set the value of the SelectionStart property to a date that is later than the current value of the SelectionEnd property, SelectionEnd is automatically set equal to SelectionStart.

If you set a date in SelectionStart that causes the selection to exceed the number of days specified by the MaxSelectionCount property, the value of SelectionEnd is adjusted; SelectionEnd is automatically set so that the number of days selected is equal to MaxSelectionCount.

NoteNote:

MaxSelectionCount represents the number of days in the selection, not the difference between SelectionStart and SelectionEnd. For example, if MaxSelectionCount is 7 (the default), then SelectionStart and SelectionEnd can be no more than six days apart.

NoteNote:

Setting the SelectionRange for a MonthCalendar control that has visual styles enabled will result in the selection range not painting correctly on the control.

The following code example demonstrates how to use the SelectionStart and SelectionEnd properties. To run the example, paste the following code into a form containing a MonthCalendar control named MonthCalendar1, and call the ShowAWeeksVacationOneMonthFromToday method from the form's constructor or Load event-handling method.

// Computes a week one month from today.
private void ShowAWeeksVacationOneMonthFromToday()
{
    System.DateTime today = this.MonthCalendar1.TodayDate;
    int vacationMonth = today.Month + 1;
    int vacationYear = today.Year;

    if (today.Month == 12)
    {
        vacationMonth = 1;
        ++vacationYear;
    }

    // Select the week using SelectionStart and SelectionEnd.
    this.MonthCalendar1.SelectionStart = 
        new System.DateTime(today.Year, vacationMonth, today.Day-1);
    this.MonthCalendar1.SelectionEnd = 
        new System.DateTime(today.Year, vacationMonth, today.Day+6);
}

// Computes a week one month from today.
private void ShowAWeeksVacationOneMonthFromToday()
{
    System.DateTime today = this.monthCalendar1.get_TodayDate();
    int vacationMonth = 1;
    if (today.get_Month() < 12) {
        vacationMonth = today.get_Month() + 1;
    }
    // Select the week using SelectionStart and SelectionEnd.
    this.monthCalendar1.set_SelectionStart(
        new System.DateTime(today.get_Year(),
        vacationMonth, today.get_Day() - 1));
    this.monthCalendar1.set_SelectionEnd(
        new System.DateTime(today.get_Year(),
        vacationMonth, today.get_Day() + 6));
} //ShowAWeeksVacationOneMonthFromToday

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Show: