|
As mentioned above, the Date Time Picker control is made a Date Picker by setting its Format property to either Short Date or Long Date. To set the Short Date option programmatically, add the
DTS_SHORTDATEFORMAT style. This type displays the numeric month, followed by the numeric day, and followed by the year. If you prefer a date that includes the names of the month and the weekday, set the Format property to Long Date. To set this option programmatically, add the
DTS_LONGDATEFORMAT style.
Unlike the Time Picker, the Date Picker is in fact two controls, combining a combo box and a calendar. Because the calendar part enjoys all the functionality of the Month Calendar control, you have the ability to manipulate, separately, any of its characteristics, as there are related to the Month Calendar control we reviewed previously. To manipulate this calendar as a control, you would need to access it first by its handle. To get a handle to the calendar of the Date Picker, call the
GetMonthCalCtrl() method. Its syntax is:
CMonthCalCtrl *GetMonthCalCtrl() constl;
By default, the Date Picker control displays a combo box. If you do not like the combo box, you can display a spin button instead. To set this option at design time, change the value of the Use Spin Control property from False (the default) to True. To programmatically set this option, add the
DTS_UPDOWN style.
When the control has the Use Spin Control property set to False and the use clicks the arrow of the control, a calendar displays to the bottom-left or the bottom-right side of the combo box. To control this alignment, change the value of the Right Align property. Its default value is set to True, which means that the calendar would be aligned to the right. To programmatically set this property, add the
DTS_RIGHTALIGN property.
The displayed Month Calendar object allows the user to select a date using the same techniques we described for the Month Calendar control. The Month Calendar of the Date Picker control displays using the same colors and other properties as we saw with the Month Calendar control. After the user has selected a date, the date value displays in the edit control of the combo box and the calendar disappears.
If the control displays a spin button, the object is divided in different sections that can each be changed individually:
To change either the day, the month, or the year, the user must click the desired section and use either the arrows of the button or the arrow keys on the keyboard to increase or decrease the selected value.
If you want to control the range of dates the user can select, call the
CDateTimeCtrl::SetRange() method. It is overloaded in two versions whose syntaxes are:
BOOL SetRange(const COleDateTime* pMinRange,const COleDateTime* pMaxRange);
BOOL SetRange(const CTime* pMinRange,const CTime* pMaxRange);
This method takes two arguments. The first is the lowest date that the user can navigate to. The second is the highest date that the user can navigate to.
To know the range of values that the user is allowed to navigate to, call the
CDateTimeCtrl::GetRange() method. It is overloaded in two versions whose syntaxes are:
DWORD GetRange(COleDateTime* pMinRange, COleDateTime* pMaxRange);
DWORD GetRange(CTime* pMinRange, CTime* pMaxRange);
When you add the DateTimePicker control to your form or container, it displays the date of the computer at the time the control was added. If you want the control to display a different date, set the desired value using the
SetTime() method. At any time, you can find out what value the Date Picker has by retrieving the value of the
TDateTimePicker::Date property.
By default, the date displays using either the Short Date or the Long Date formats of Control Panel. If you want to customize the way the date is displayed, call the
CDateTimeCtrl::SetFormat() method. The letters used to create a formats are as follows:
|