DateTimePicker Control
The DateTimePicker control (see output of Fig. 14.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. 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.
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 user's options. |
Format |
Sets the format of the date and/or time used for the user's options. |
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 |
Used to indicate that the control should have up and down Buttons. This is helpful for instances when the DateTimePicker is used to select a timethe Buttons can be used to increase or decrease hour, minute and second values. |
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 14.11 demonstrates using the DateTimePicker control to select an item's drop-off time. 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.
1 // Fig. 14.11: DateTimePickerForm.cs 2 // Using a DateTimePicker to select a drop off time. 3 using System; 4 using System.Windows.Forms; 5 6 public partial class DateTimePickerForm : Form 7 { 8 // default constructor 9 public DateTimePickerForm() 10 { 11 InitializeComponent(); 12 } // end constructor 13 14 private void dateTimePickerDropOff_ValueChanged( 15 object sender, EventArgs e ) 16 { 17 DateTime dropOffDate = dateTimePickerDropOff.Value; 18 19 // add extra time when items are dropped off around Sunday 20 if ( dropOffDate.DayOfWeek == DayOfWeek.Friday || 21 dropOffDate.DayOfWeek == DayOfWeek.Saturday || 22 dropOffDate.DayOfWeek == DayOfWeek.Sunday ) 23 24 //estimate three days for delivery 25 outputLabel.Text = dropOffDate.AddDays( 3 ).ToLongDateString(); 26 else 27 // otherwise estimate only two days for delivery 28 outputLabel.Text = dropOffDate.AddDays( 2 ).ToLongDateString(); 29 } // end method dateTimePickerDropOff_ValueChanged 30 31 private void DateTimePickerForm_Load( object sender, EventArgs e ) 32 { 33 // user cannot select days before today 34 dateTimePickerDropOff.MinDate = DateTime.Today; 35 36 // user can only select days of this year 37 dateTimePickerDropOff.MaxDate = DateTime.Today.AddYears( 1 ); 38 } // end method DateTimePickerForm_Load 39 } // end class DateTimePickerForm (a) (b) (c) (d) |
The DateTimePicker (dateTimePickerDropOff) 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 1429) first retrieves the selected date from the DateTimePicker's Value property (line 17). Lines 2022 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 25 and 28 use DateTime's AddDays method to increase the date by two days or three days, respectively. The resulting date is then displayed in Long format using 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 34 and 37). 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 selected the 12th of July. In Fig. 14.11(c), the estimated arrival date is displayed as the 14th. Figure 14.11(d) shows that the 12th, after it is selected, is highlighted in the calendar.
LinkLabel Control
|