Section 14.4. DateTimePicker Control


14.4. DateTimePicker Control

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

  1  ' Fig. 14.11: FrmDateTimePickerForm.vb  2  ' Using a DateTimePicker to select a drop off date.  3  Public Class FrmDateTimePickerTest  4     ' set DateTimePicker's MinDate and MaxDate properties  5     Private Sub FrmDateTimePickerTest_Load(ByVal sender As System.Object, _  6        ByVal e As System.EventArgs) Handles MyBase.Load  7        ' user cannot select days before today  8        dropOffDateTimePicker.MinDate = DateTime.Today  9 10        ' user can select days up to one year from now 11        dropOffDateTimePicker.MaxDate = DateTime.Today.AddYears(1) 12     End Sub ' FrmDateTimePickerTest_Load 13 14     ' display delivery date 15     Private Sub dropOffDateTimePicker_ValueChanged( _ 16        ByVal sender As System.Object, ByVal e As System.EventArgs) _ 17        Handles dropOffDateTimePicker.ValueChanged 18 19        Dim dropOffDate As DateTime = dropOffDateTimePicker.Value 20 21        ' add an extra day when items are dropped off Friday-Sunday 22        If dropOffDate.DayOfWeek = DayOfWeek.Friday Or _ 23           dropOffDate.DayOfWeek = DayOfWeek.Saturday Or _ 24           dropOffDate.DayOfWeek = DayOfWeek.Sunday Then 25           ' estimate three days for delivery 26           lblOutput.Text = dropOffDate.AddDays(3).ToLongDateString() 27        Else ' otherwise estimate only two days for delivery 28           lblOutput.Text = dropOffDate.AddDays(2).ToLongDateString() 29        End If 30     End Sub ' dateTimePickerDropOff_ValueChanged 31  End Class ' FrmDateTimePickerTest 

(a)

(b)

(c)

(d)

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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net