14.4. DateTimePicker ControlThe DateTimePicker control (see output of Fig 14.11) is similar to the MonthCalendar control, but displays the calendar when the user clicks the down arrow. The DateTimePicker can be used to retrieve date and time information from the user. The DateTimePicker is also more customizable than a MonthCalendar controlmore properties are provided to edit the look-and-feel of the drop-down calendar. Property Format specifies the user's selection options using the DateTimePickerFormat enumeration. The values in this enumeration are Long (displays the date in long format, as in Friday, July 1, 2005), Short (displays the date in short format, as in 7/1/2005), Time (displays a time value, as in 11:48:02 PM) and Custom (indicates that a custom format will be used). If value Custom is used, the display in the DateTimePicker is specified using property CustomFormat. The default event for this control is ValueChanged, which occurs when the selected value (whether a date or a time) is changed. DateTimePicker properties and a common event are summarized in Fig. 14.10.
Figure 14.11 demonstrates using the DateTimePicker control to select an item's drop-off date. Many companies use such functionality. For instance, several online DVD-rental companies specify the day a movie is sent out, and the estimated time that the movie will arrive at your home. In this application, the user selects a drop-off day, and then an estimated arrival date is displayed. The date is always two days after drop off, three days if a Sunday is reached (mail is not delivered on Sunday). Figure 14.11. Demonstrating DateTimePicker.
The DateTimePicker (dropOffDateTimePicker) has its Format property set to Long, so the user can select a date and not a time in this application. When the user selects a date, the ValueChanged event occurs. The event handler for this event (lines 1530) first retrieves the selected date from the DateTimePicker's Value property (line 19). Lines 2224 use the DateTime structure's DayOfWeek property to determine the day of the week on which the selected date falls. The day values are represented using the DayOfWeek enumeration. Lines 26 and 28 use DateTime's AddDays method to increase the date by three days or two days, respectively. Then a string representing the delivery date is obtained by calling method ToLongDateString. In this application, we do not want the user to be able to select a drop-off day before the current day, or one that is more than a year into the future. To enforce this, we set the DateTimePicker's MinDate and MaxDate properties when the Form is loaded (lines 8 and 11). Property Today returns the current day, and method AddYears (with an argument of 1) is used to specify a date one year in the future. Let's take a closer look at the output. This application begins by displaying the current date (Fig. 14.11(a)). In Fig. 14.11(b), we select the 12th of July. In Fig. 14.11(c), the estimated arrival date is displayed as the 14th. Fig. 14.11(d) shows that the 12th, after it is selected, is highlighted in the calendar. |