Ever since computers were invented, data validation has been an important concern. When it comes to a user interface, the fastest way to validate data is whilst it is being input. Strange, then, that there is no standard Windows edit control that tackles this problem for any kind of data.
This validating edit control aims to provide the ultimate Framework for data validation, no matter how complex the data you are attempting to input is.
- Validation must be clean and reliable
- The control should be usable exactly like
- Only a handful of visual effects will be included as standard
- 'WM_KILLFOCUS is the wrong time to do field validation'
- Auto-formatting should be easy to add from a derived class
FilterEdit must stay as simple as possible and not get too bloated!
Intercepted Windows Messages
The following Windows messages are trapped for validation purposes:
... and these are trapped to perform the visual effects:
Windows Message Overrides for Validation
WM_CHAR is the Windows message that every validating edit control ever written for Windows must have trapped. This is where a single character can be checked to see if the control will accept it or not.
WM_KEYDOWN is where the Delete key, Ctrl-X, Ctrl-C and Ctrl-V are trapped.
WM_KILLFOCUS is a very popular message to trap. However, rather than trying to set the focus back to the control in the event that the input is incomplete, we just flag the error and allow the focus to switch normally.
WM_SETFOCUS is trapped simply so that we can set any colours we need to and display a tooltip if required.
This is where the rest of the Windows messages we are interested in are processed.
How to Derive Your Own Custom Control
Here are the four obvious things you might want to do:
CUIntRangeEdit example to see how this works.
By trapping characters yourself, you can automatically format input and perform semantic validation as the user types. Refer to the
CDateTimeEdit example to see how this works.
You may want to perform extra formatting when the user leaves the control. Again, see the
CDateTimeEdit example. Don't forget to call
CBaseEdit::OnKillFocus if you do this!
This is if you want to pre-process the
string before running the syntax checking. See
CDateTimeEdit for an example.
Example Controls Included in the ZIP
You might want to explain the data format for your controls at runtime. For this reason, Balloon Help support is included. To include this support, you must call
m_DateEdit.CreateToolTip (this, _T("Tooltip text here"));
The C++ Standard Regular Expression Library
boost::regex library has been accepted into the C++ Standard Library. Due to its partial match support, the library is also ideal for this control. Get the Boost library here.
- Add more demo controls
- Tutorial for deriving controls
All feedback is most welcome!
- 18.104.22.168 - 30th July, 2004
- 22.214.171.124 - 8th August, 2007
- BUG FIX: Now using
- 126.96.36.199 - 22nd August, 2007
- Now allows user to delete the entire contents of an edit control, regardless of the regex
- 188.8.131.52 - 14th January, 2008
CDateEdit - now
- 184.108.40.206 - 25th February, 2008
- 220.127.116.11 - 13th June, 2008
bool CDateTimeEdit::GetTimeStamp (SQL_TIMESTAMP_STRUCT *pTS)
- Filter out Ctrl-X, Ctrl-C and Ctrl-V in
- 18.104.22.168 - 8th August, 2008
- Display tooltip right at the bottom of the edit control to avoid painting overwriting spin control border!
- 22.214.171.124 - 15th September, 2008
- Use system colours as defaults, support
- 126.96.36.199 - 15th October, 2008
- Dynamic calendar dialog and bitmap for
CDateTimeEdit (no resource file entries required now)
- 188.8.131.52 - 7th November, 2008
- Fixed assertions in
- 184.108.40.206 - 5th December, 2008
posix_time support to
- 220.127.116.11 - 9th January, 2009
WM_SETTEXT to set an empty string even if regex forbids it
- 18.104.22.168 - 1st May, 2009
- Interface change for Date/Time
- 22.214.171.124 - 7th September, 2009
- Bug fix to
- 126.96.36.199 - 19th May, 2010
- Adds unsigned support to
- 188.8.131.52 - 7th October, 2010
- This version extends
CFloatEdit so that you can specify the number of digits for the whole part
- 184.108.40.206 - 25th October, 2010
- This version fixes a bug in
- 220.127.116.11 - 26th January, 2011
- This version adds an HHMM mode for times
- 18.104.22.168 - 17th February, 2011
- This version adds the
BS_DEFPUSHBUTTON style to the calendar button in
- 22.214.171.124 - 21st February, 2011
- Changed the date ordering for
set functions in
CDateTimEdit to bring it in line with
- 126.96.36.199 - 8th April, 2011
- This update fixes range checking in
- 188.8.131.52 - 12th September, 2011
- This version adds
CDateTimeEdit and fixes some bugs
- 184.108.40.206 - 3rd October, 2011
- This version fixes a bug in