DateTimePicker
ControlThe DateTimePicker
control (see output of Fig. 15.11) is similar to the MonthCalendar
control but displays the calendar when a down arrow is selected. The DateTimePicker
can be used to retrieve date and time information from the user. A DateTimePicker
’s Value
property stores a DateTime
object, which always contains both date and time information. You can retrieve the date information from the DateTime
object by using property Date
, and you can retrieve only the time information by using the TimeOfDay
property.
The DateTimePicker
is also more customizable than a MonthCalendar
control— more 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 Thursday, July 10, 2013), Short
(displays the date in short format, as in 7/10/2013), Time
(displays a time value, as in 5:31: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. 15.10.
DateTimePicker
properties and an event.DateTimePicker properties and an event |
Description |
---|---|
DateTimePicker Properties |
|
CalendarForeColor |
Sets the text color for the calendar. |
CalendarMonthBackground |
Sets the calendar’s background color. |
CustomFormat |
Sets the custom format string for the date and/or time displayed in the control. |
Format |
Sets the format of the date and/or time used for the date and/or time displayed in the control. |
MaxDate |
The maximum date and time that can be selected. |
MinDate |
The minimum date and time that can be selected. |
ShowCheckBox |
Indicates if a CheckBox should be displayed to the left of the selected date and time. |
ShowUpDown |
Indicates whether the control displays up and down Button s. Helpful when the DateTimePicker is used to select a time—the Button s can be used to increase or decrease hour, minute and second. |
Value |
The data selected by the user. |
Common DateTimePicker Event |
|
ValueChanged |
Generated when the Value property changes, including when the user selects a new date or time. |
Figure. 15.11 demonstrates using a DateTimePicker
to select an item’s drop-off time. Many companies use such functionality—online retailers typically specify the day a package is sent out and the estimated time that it will arrive at your home. The user selects a drop-off day, 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).
The DateTimePicker
(dropOffDateTimePicker
) has its Format
property set to Long
, so the user can select just a date in this app. When the user selects a date, the ValueChanged
event occurs. The event handler for this event (lines 18–36) first retrieves the selected date from the DateTimePicker
’s Value
property (line 21). Lines 24–26 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 29 and 34 use Date-Time
’s AddDays
method to increase the date by three days or two days, respectively. The resulting date is then displayed in Long
format using method ToLongDateString
.
In this app, we do not want the user to be able to select a drop-off day before the current day, or one that’s more than a year into the future. To enforce this, we set the Date-TimePicker
’s MinDate
and MaxDate
properties when the Form
is loaded (lines 41 and 44). 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 app begins by displaying the current date (Fig. 15.11(a)). In Fig. 15.11(b), we selected the 23rd of June. In Fig. 15.11(c), the estimated arrival date is displayed as the 25th of June. Figure 15.11(d) shows that the 23rd, after it is selected, is highlighted in the calendar.