How to stop typing in CDateTimeCtrl in MFC
-
I have a CDateTimeCtrl on my dialog. Through this I can choose any date in given range from drop down. But currently user is also allowed to press numeric keys to set the date. Is there any way to stop user from typing in it. I want, he should only be able to select the date from drop-down and I don't want to disable the control. I have checked the "AllowEdit" property but that is different thing. Currently it is set to false.
-
I have a CDateTimeCtrl on my dialog. Through this I can choose any date in given range from drop down. But currently user is also allowed to press numeric keys to set the date. Is there any way to stop user from typing in it. I want, he should only be able to select the date from drop-down and I don't want to disable the control. I have checked the "AllowEdit" property but that is different thing. Currently it is set to false.
You may derive a class from
CDateTimeCtrl()
and add aWM_CHAR
/OnChar()
handler that does nothing or calls the base class handler for non-digit chars only. I've just tested it. It does not effect keys pressed in the drop down calendar and still allows cursor keys and TAB in the edit control. -
You may derive a class from
CDateTimeCtrl()
and add aWM_CHAR
/OnChar()
handler that does nothing or calls the base class handler for non-digit chars only. I've just tested it. It does not effect keys pressed in the drop down calendar and still allows cursor keys and TAB in the edit control.Thanks Jochen, you are right. That way it will work. But for such a small requirement, I don't want to do big changes :doh: and also I don't want to go for pretranslate message. Is there any way to handle WM_CHAR/OnChar events for the date control on the same dialog class?
-
Thanks Jochen, you are right. That way it will work. But for such a small requirement, I don't want to do big changes :doh: and also I don't want to go for pretranslate message. Is there any way to handle WM_CHAR/OnChar events for the date control on the same dialog class?
You are using MFC, so you don't need to handle
PreTranslateMessage()
. Just use the wizard to derive a class, use the message properties to add the WM_CHAR handler, and comment the line callingCDateTimeCtrl::OnChar()
. Done with a few mouse clicks. All you need to type in using the keyboard is the name of the drived class, the comment chars, and replacing theCDateTimeCtrl
by your class name in your code. The edit control ofCDateTimeCtrl
is not accessible. So it can't be done in the dialog. Even when it would be possible: It is always better to perform such things in the control rather than in the dialog. So it must be done only once and not within every dialog that uses such a control.