简体   繁体   中英

Win32: How to prevent DateTimePicker from accepting 2-digit years?

Microsoft's DateTimePicker common control shows ShortDate 's according to the configured Locale settings. The customer's computer is configured with a 2-digit year ShortDateFormat ( d/M/yy ), so the DateTimePicker shows 2-digit years:

替代文字

When the user puts focus in the year block of the datetime picker, Microsoft points out the issue, and shows the full 4-digit year:

替代文字

But then the user, wanting to enter 1929 as a year, types a 2-digit year:

替代文字

And then when they tab out they're confused when Windows interprets their 2-digit years as being between 1930 and 2029 , and it is assumed to be 2029:

替代文字

The customer has a global corporate policy in place which dictates that Windows regional settings be configured for 2-digit years.

So as a workaround i need to force every DateTimePicker control to use a custom date-format, using the handy DateTime_SetFormat macro :

DateTime_SetFormat(hwnd_DateOfEventPicker, "d/M/yyyy");

This helps, so that now the DateTimePicker now always shows a 4-digit year:

替代文字

The problem is that the user can still type in an " invalid " 2-digit year:

替代文字

Which Windows is still intpreting, according to the rules, as 2029:

替代文字

i need the date-time picker to reject 2-digit years.

If this can't work, then i'm going to be forced to replace every DateTimePicker with 3 edit boxes:

22 6 29

Where i can color invalid numbers red, and point out the invalid data entry by the user.

How can i prevent the DateTimePicker from accept y2k bugs?

如果添加DTS_APPCANPARSE窗口样式,则可以处理WM_NOTIFY:DTN_USERSTRING消息并自己解析字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM