Introduction
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
Regular
Expression control
that tackles this problem for any
kind of data.
This validating
Edit
Regular Expression control
aims to provide the ultimate
Framework for data validation, no
matter how complex the data you are
attempting to input is.
Design Decisions
-
Validation must be clean and
reliable
-
The control should be usable
exactly like
CEdit
Regular
Expression
-
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
Regular Expression
must
stay as simple as possible and
not get too bloated!
Intercepted Windows Messages
The following Windows messages are
trapped for validation purposes...
EM_REPLACESEL
WM_CLEAR
WM_CHAR
WM_CUT
WM_KEYDOWN
WM_KEYUP
WM_KILLFOCUS
WM_PASTE
WM_SETFOCUS
WM_SETTEXT
... and these are trapped to perform
the visual effects:
Windows Message Overrides for
Validation
CBaseEdit
Regular Expression ::OnChar
WM_CHAR
is the Windows message that every
validating Edit
Regular
Expression 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.
CBaseEdit Regular Expression
::OnKeyDown
WM_KEYDOWN
is where the Delete key, Ctrl-X,
Ctrl-C and Ctrl-V are trapped.
CBaseEdit Regular Expression
::OnKillFocus
Again,
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.
CBaseEdit Regular Expression
::OnSetFocus
WM_SETFOCUS
is trapped simply so that we can set
any colours we need to and display a
tooltip if required.
CBaseEdit
Regular Expression
::WindowProc
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:
-
Override
SemanticCheck
See the
CUIntRangeEdit
Regular Expression
example
to see how this works.
-
Trap
WM_CHAR
yourself
By trapping characters yourself,
you can automatically format
input and perform semantic
validation as the user types.
Refer to the
CDateTimeEdit Regular Expression
example
to see how this works.
-
Trap
WM_KILLFOCUS
yourself
You may want to perform extra
formatting when the user leaves
the control. Again, see the
CDateTimeEdit
Regular
Expression
example.
Don't forget to call
CBaseEdit
Regular Expression
::OnKillFocus
if you do this!
-
Override
SyntaxCheck
This is if you want to
pre-process the
string
before running the syntax
checking. See
CDateTimeEdit
Regular
Expression
for
an
example.
Example
Controls Included in the ZIP
CCurrencyEdit
CDateTimeEdit
CFloatEdit
CIntEdit
CUIntEdit
CUIntRangeEdit
CSpin
Tooltip Support
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
CreateToolTip
:
BOOL CEdit TestDlg::OnInitDialog()
{
CDialog::OnInitDialog ();
...
m_DateEdit .CreateToolTip (this, _T("Tooltip text here"));
...
}
The C++
Standard
Regular Expression Library
The
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.