![]() |
Using a Month Calendar Control |
|
Selecting a Date |
|
As mentioned in our description, the calendar control displays the days of a selected month. The control also displays the remaining days, if any, of the first week of the currently selected month; that is, the days of the previous month that share the week with the first day of the first week of the selected month. The control also displays the first days of the subsequent month that share the week with the last day of the current month. |
|
To use the calendar control, the user can click a date, whether it a date from the current month or a day of the other (previous and next) month. When the user has clicked a date to select it, the control fires a DateSelected event. The DateSelected event is of type DateRangeEventArgs. The user can also select a date using the keyboard. To do this, the user must first give focus to the control. This is possible by pressing Tab continuously until the control receives focus (or by clicking any date on the control. To select a date using the keyboard, the user can continually press one of the arrow keys (on the keyboard) until the desired date is selected. You too can programmatically select a date on the calendar control. To do this, assign a valid DateTime value to both the SelectionStart and the SelectionEnd properties. Here is an example: private void btnSelect_Click(object sender, EventArgs e)
{
monthCalendar1.SelectionStart = new DateTime(1988, 12, 5);
monthCalendar1.SelectionEnd = new DateTime(1988, 12, 5);
}
When a date has been selected, whether by the user (using the mouse or the keyboard) or by you (through code), the control fires a DateChanged event. The DateChanged event is of type DateRangeEventArgs. The DateRangeEventArgs class is equipped with two properties: Start and End. When the user clicks a date, these two properties hold the date that was clicked. This means that you can use either of these properties to know the date that was clicked. Both the Start and the End properties are of type DateTime.
When the user clicks the calendar control, one date is selected. As mentioned in our description, you can give the control the ability to display more than one month. To make this possible, when creating the control, set its width to have enough space. In the same way, you can increase the height to display many months. To select more than one date, the user can click one date, hold the mouse down, and drag to the left, the right, up or down: ![]() The user can also select a range of dates using the keyboard or using a combination of the mouse and the keyboard. To do this, the user must first give focus to the control. To select a range of dates using the keyboard, the user can press and hold Shift, then press one of the arrow keys continually until the last date of the desired range is selected (in reality, we will see that there is a property that controls the maximum range of dates that can be selected). To select a range of dates using a combination of the mouse and keyboard, the user can click the first date, press and hold Shift, then click the last date. After selecting the days, the starting date is stored in the SelectionStart property. The last date of the selection is stored in the SelectionEnd property. Both properties are of DateTime type. The range of the selected dates is stored in a SelectionRange value. SelectionRange is simply a class that can give you information about the beginning and the end of a selected range of dates. To programmatically select a date, assign the starting to the SelectionStart property and assign the last date to the SelectionEnd property. By default, the user can select only up to 7 days at a time. If you want the user to be able to select more or less days than that. Here is an example where the user have selected dates from the 11 to the 20th, that is 10 days:
If you configure the control to display more than one month, the user can select days from one month to another as long as the days are in a range. Here is an example of a calendar that displays two months and the user had selected dates from September 21st, 2007 to October 11th, 2007:
After selecting a range of dates, the control fires a DateChanged event, which is of type DateRangeEventArgs. We saw earlier that the DateRangeEventArgs class has two properties. The DateRangeEventArgs.Start property holds the starting date of the range that the user made. The DateRangeEventArgs.End holds the last date from the range that the user made.
To control the number of days you want the user to be able to select, change the value of the MaxSelectionCount property. The user cannot select more days than the MaxSelectionCount value but the user can select less.
|
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Next |
|
|
||