15.4 DateTimePicker Control

The 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.

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 Buttons. Helpful when the DateTimePicker is used to select a time—the Buttons 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).

Fig. 15.11 Using a DateTimePicker to select a drop-off time.

Alternate View

  1    // Fig. 15.11: DateTimePickerForm.cs
  2    // Using a DateTimePicker to select a drop-off time.
  3    using System;
  4    using System.Windows.Forms;
  5
  6    namespace DateTimePickerTest
  7    {
  8       // Form lets user select a drop-off date using a DateTimePicker
  9       // and displays an estimated delivery date
 10       public partial class DateTimePickerForm : Form
 11       {
 12          // constructor
 13          public DateTimePickerForm()
 14          {
 15             InitializeComponent();
 16          }
 17
 18          private void dropOffDateTimePicker_ValueChanged(
 19             object sender, EventArgs e)
 20          {
 21             DateTime dropOffDate = dropOffDateTimePicker.Value;
 22
 23              // add extra time when items are dropped off Sunday
 24              if (dropOffDate.DayOfWeek == DayOfWeek.Friday ||
 25                 dropOffDate.DayOfWeek == DayOfWeek.Saturday ||
 26                 dropOffDate.DayOfWeek == DayOfWeek.Sunday)
 27              {
 28                 //estimate three days for delivery
 29                 outputLabel.Text = dropOffDate.AddDays(3).ToLongDateString();
 30              }
 31              else
 32              {
 33                 // otherwise estimate only two days for delivery
 34                 outputLabel.Text = dropOffDate.AddDays(2).ToLongDateString();
 35              }
 36            }
 37
 38            private void DateTimePickerForm_Load(object sender, EventArgs e)
 39            {
 40               // user cannot select days before today
 41               dropOffDateTimePicker.MinDate = DateTime.Today;
 42
 43               // user can only select days up to one year in the future
 44               dropOffDateTimePicker.MaxDate = DateTime.Today.AddYears(1);
 45             }
 46         }
 47    }

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset