MFC Controls - The Date Picker

 

Overview

The Date and Time Picker is a control that allows the user to select either a date or a time value. This control provides two objects in one:

One of the advantages of the Date and Time Picker control is that it allows the user to select a time or a date value instead of typing it. This tremendously reduces the likelihood of mistakes.

To create a Date or Time Picker control, add a Date Time Picker control to a dialog box or a form. To programmatically create a Date Time Picker control, declare CDateTimeCtrl variable and use its Create() method to initialize it.

Introduction to the Date Picker Control

After adding the DateTimePicker control to the form, to allow the user to set the dates and not the times on the control, you can set its Format property to either Short Date (which is the default of the control) or Long Date. This would change the control into a combo box.

If the control displays a combo box, if the user clicks the arrow on the Date control, a calendar object similar to the Month Calendar control we saw earlier displays:

 

Characteristics of the Date Picker Control

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:

Format Used For Description
d Days Displays the day as a number from 1 to 31
dd Days Displays the day as a number with a leading 0 if the number is less than 10
ddd Weekdays Displays a weekday name with 3 letters as Mon, Tue, etc
dddd Weekdays Displays the complete name of a week day as Monday, etc
M Months Displays the numeric month from 1 to 12
MM Months Displays the numeric month with a leading 0 if the number is less than 10
MMM Months Displays the short name of the month as Jan, Feb, Mar, etc
MMMM Months Displays the complete name of the month as January, etc
yy Years Displays two digits for the year as 00 for 2000 or 03 for 2003
yyyy Years Displays the numeric year with 4 digits

To retrieve the current format used on the control, you can call its GetFormat() method.

The user may want to edit the date value of the control, including typing the month, day, year, the name of the month or the name of the weekday. The Date Picker object is equipped to control the types of values that can be entered. For example, the user cannot type the name of a month, only a number and the control would display the corresponding name of the month. This is the default scenario where you let this object help you control the values that the user can type. If you want to take matters into your own hands, that is, if you want to allow the user to type the value of the date or time of the control, at design time set the Allow Edit Boolean property to True (its default value is False). To set this option programmatically, add the DTS_APPCANPARSE style.

If you allow the user to manually enter the date value, if the value is incorrect, when the control looses focus, the compiler would make an attempt to convert the date entered into a valid and true date. If the user enters an invalid value, the compiler would throw an error. This means that you should be reluctant to let the users type whatever they want. The less they type, the less checking you need to do.

 
 

Copyright © 2004-2007 FunctionX, Inc.