DateTimePicker Format changes from Time to Short Date

E

Edward Mitchell

I have a number of DateTimePicker controls, some set to dates, some set to a
format of Time. The controls are all embedded in dialogs. I created the
controls by dragging the DateTime picker from the Toolbox and then set the
Format property appropriately.

I have noticed that sometimes the Time format will reset spontaneously to
Short Date. I looked at the .rc file and found that the usual form for a
Short Date is as follows:

CONTROL "DateTimePicker1",IDC_REGATTA_DATE,"SysDateTimePick32",
DTS_UPDOWN | WS_TABSTOP,116,77,64,14

The text for a format of Time is as follows:

CONTROL "DateTimePicker2",IDC_CREATE_START_TIME,
"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN |
DTS_SHOWNONE | WS_TABSTOP | 0x8,8,131,70,14

so I deduced that the DateTimePicker1 indicates a date and DateTimePicker2
indicates a time.

However, when I edit a date control and change the format into a Time, the
..rc file then contains the following:

CONTROL "DateTimePicker1",IDC_REGATTA_DATE,"SysDateTimePick32",
DTS_UPDOWN | WS_TABSTOP | 0x8,116,77,64,14

and the only difference that I can see is the bar that separates the
WS_TABSTOP from the string of numbers.

I assume all the format info must be embedded into the .rc file text so I
can't see in this case what the real difference is between the Short Date
format and the Time format. However, when the resource editor reads the .rc
file it should be able to figure out the correct format. I wonder if this
difference between the use of the ...Picker2 and ...Picker1 can make the
editor make the wrong decision about which format to use when the properties
are displayed.

Is there a known problem with the format for the DateTimePicker or is there
anything I can do to keep the format the same?

I am running on XP using C++ from .NET 2003. I have an unmanaged project
that was converted from VS6.0.

Ed
--
Edward E.L. Mitchell
Web: www.racesail.org
Phone: (239)415-7039
6707 Daniel Court
Fort Myers, FL 33908
 
J

Jeff Partch [MVP]

Edward Mitchell said:
I have a number of DateTimePicker controls, some set to dates, some set to a
format of Time. The controls are all embedded in dialogs. I created the
controls by dragging the DateTime picker from the Toolbox and then set the
Format property appropriately.

I have noticed that sometimes the Time format will reset spontaneously to
Short Date. I looked at the .rc file and found that the usual form for a
Short Date is as follows:

CONTROL "DateTimePicker1",IDC_REGATTA_DATE,"SysDateTimePick32",
DTS_UPDOWN | WS_TABSTOP,116,77,64,14

The text for a format of Time is as follows:

CONTROL "DateTimePicker2",IDC_CREATE_START_TIME,
"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN |
DTS_SHOWNONE | WS_TABSTOP | 0x8,8,131,70,14

so I deduced that the DateTimePicker1 indicates a date and DateTimePicker2
indicates a time.

However, when I edit a date control and change the format into a Time, the
.rc file then contains the following:

CONTROL "DateTimePicker1",IDC_REGATTA_DATE,"SysDateTimePick32",
DTS_UPDOWN | WS_TABSTOP | 0x8,116,77,64,14

and the only difference that I can see is the bar that separates the
WS_TABSTOP from the string of numbers.

I assume all the format info must be embedded into the .rc file text so I
can't see in this case what the real difference is between the Short Date
format and the Time format. However, when the resource editor reads the ..rc
file it should be able to figure out the correct format. I wonder if this
difference between the use of the ...Picker2 and ...Picker1 can make the
editor make the wrong decision about which format to use when the properties
are displayed.

Is there a known problem with the format for the DateTimePicker or is there
anything I can do to keep the format the same?

The difference is that 0x8 being ORd with the styles. In theory it should be
DTS_TIMEFORMAT which the SDK headers say is 0x0009, but I think that is
0x8|DTS_UPDOWN. I don't know why the IDE writes it this way. Anyway, I can
repro that the IDE looses this style on reload of the resource. I suspect
it's a bug, but I don't know if its known or what a workaround could
possibly be -- other than not letting the IDE touch the resource, or not
using VS2003 for development until they get it fixed.
 
D

David Lowndes

I suspect
it's a bug, but I don't know if its known or what a workaround could
possibly be

It's a bug that MS have known about for ages. There's no workaround
that I know of, other than to keep hand editing the RC file - or as
I've ended up doing, ensure the control is created with the correct
style in code :(

Dave
 
R

Ronald Laeremans [MSFT]

I asked the team that owns this to take a look and enter the bug if it
wasn't in our database yet.

Ronald Laeremans
Visual C++ team
 
E

Edward Mitchell

The DTS_TIMEFORMAT is defined as 0x0009. There is a 0x0008 (single bit)
missing in the DTS_ definitions and the 0x0009 seems to clash with the
DTS_UPDOWN if they really are single bit selectors!

Can I enforce the style with something like (in the OnInitDialog):

CDateTimeCtrl* pTime =
(CDateTimeCtrl*)GetDlgItem(IDC_CREATE_START_TIME);
DWORD dwStyle = pTime->GetStyle();
pTime->SetStyle((dwStyle & ! DTS_SHORTDATEFORMAT) | DTS_TIMEFORMAT);
...

I assume that this will ensure that the style is always the TIME format.

Ed
 
T

Tian Min Huang

Hello Ed,

Thanks for your response. I did not find SetStyle method of CDateTimeCtrl,
anyway, you can call ModifyStyle to change a window's style. Please refer
to the following code:

//--------------code snippet-------------
CDateTimeCtrl* pTime = (CDateTimeCtrl*)GetDlgItem(IDC_CREATE_START_TIME);
pTime->ModifyStyle(DTS_SHORTDATEFORMAT, DTS_TIMEFORMAT);
//------------------end of-------------------

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeff Partch [MVP]

Edward Mitchell said:
The DTS_TIMEFORMAT is defined as 0x0009. There is a 0x0008 (single bit)
missing in the DTS_ definitions and the 0x0009 seems to clash with the
DTS_UPDOWN if they really are single bit selectors!

As I tried to explain in my original reply, I think this 0x0009 is arrived
at by using 0x0008|DTS_UPDOWN as the 'time format' style logically includes
the 'updown' style. So it's not a clash, it's an enforcement.
Can I enforce the style with something like (in the OnInitDialog):

CDateTimeCtrl* pTime =
(CDateTimeCtrl*)GetDlgItem(IDC_CREATE_START_TIME);
DWORD dwStyle = pTime->GetStyle();
pTime->SetStyle((dwStyle & ! DTS_SHORTDATEFORMAT) | DTS_TIMEFORMAT);
...

I assume that this will ensure that the style is always the TIME format.

I realize that either this solution or Dave's dynamic creation suggestion
are about the only options, but it's still a horrible thing to have to do to
compensate for a bug in the dialog editor. The thing that really gets me is
that it seems to be thoughtfully written to add that compromised 0x08 style
value and include the DTS_UPDOWN style, but thoughtlessly written to ignore
its own scheme as well as the real DTS_TIMEFORMAT style. Awful.
 
E

Edward Mitchell

I appreciate the feedback. I used ModifyStyle(...) for the three time
controls and it works like a charm.

Hopefully I can remove these lines when the next .NET comes out!

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top