There are two types of pickers—date-time pickers, and month calendar controls. You can see them in Figure 8.4; the date-time picker is the drop-down list box at the top, and the month calendar control appears beneath it.
You can set a date and time in a date-time picker just by editing the displayed values in the control; if you click the arrow in the date-time picker, it displays a month calendar, just as a combo box would display a drop-down list; you can make selections just by clicking the calendar.
You can limit the date and times that can be selected in a date-time picker by setting the MinDate and MaxDate properties. And you can change the look of the calendar part of the control by setting the CalendarForeColor, Calendar Font, CalendarTitleBackColor, CalendarTitleForeColor, CalendarTrailing ForeColor, and CalendarMonthBackground properties.
When the user makes a selection, the new selection appears in the text box part of the date-time picker, and a ValueChanged event occurs. You can get the new text in the text box part of the control with the Text property, or as a Visual Basic DateTime object with the Value property. DateTime objects are good to know about. (We'll use them in several places in this chapter.) You can find the static properties of this class (the class properties that you can use with the class alone, without needing to create a DateTime object) in Table 8.1, the public properties of DateTime objects in Table 8.2, and the public object methods in Table 8.3.
Property |
Means |
---|---|
Now |
Holds a DateTime object holding the current local time. |
Today |
Holds a DateTime object holding the current date. |
UtcNow |
Holds a DateTime object holding the current local time in universal time (UTC). |
Property |
Means |
---|---|
Date |
Holds the date of this object. |
Day |
Holds the day of the month of this object. |
DayOfWeek |
Holds the day of the week of this object. |
DayOfYear |
Holds the day of the year of this object. |
Hour |
Holds the hour part of the date of this object. |
Millisecond |
Holds the milliseconds (thousandths of a second) part of the time represented by this object. |
Minute |
Holds the minutes of the time in this object. |
Month |
Holds the month part of the date in this object. |
Second |
Holds the seconds part of the date in this object. |
Ticks |
Holds the number of 100-nanosecond ticks that represent the date and time of this object. |
TimeOfDay |
Holds the time of day for this object. |
Year |
Holds the year part of the date in this object. |
The Format property sets the DateTimePickerFormat property of the control. The default date Format is DateTimePickerFormat.Long. If the Format property is set to DateTimePickerFormat.Custom, you can create your own format style by setting the CustomFormat property and using a custom format string. The custom format string can be a combination of custom field characters and other literal characters; see "Setting Date-Time Picker Custom Formats" later in this chapter.
To use an up-down style control to adjust the date-time value, set the ShowUpDown property to True. In this case, the calendar control will not drop down when the control is selected—here, the date and time can be set by selecting each item individually and using the up and down buttons in the control.
The month calendar control allows the user to select a date and time visually, as you can see in Figure 8.4. You can limit the date and times that can be selected by setting the MinDate and MaxDate properties. When a new date is selected, a DateSelected event occurs, and when the date is changed, a DateChanged event occurs. Because the user can use the mouse to select a range of dates, you can use the month calendar control's SelectionRange property to determine which date or dates have been selected.
The SelectionRange property returns a SelectionRange object. This has two useful properties: Start and End, which return DateTime objects corresponding to the start and end of the selected date. If only one date has been selected, both Start and End will point to that date. For example, the currently selected starting day in a month calendar control would be MonthCalendar1.Selection Range.Start.Day.
You also can use the month calendar control's SelectionStart and SelectionEnd properties to directly access the start and end dates selected without using the SelectionRange properties at all. Both of these properties return DateTime objects as well. And note also that you can change the look of the calendar part of the control by setting the ForeColor, font, TitleBackColor, TitleForeColor, TrailingForeColor, and BackColor properties.