Introduction
This article presents an abstract class, TEditTimedUpdate
, derived from MFC's CEdit
that provides periodic validation and reformatting functionality. When the user has not typed into the edit
control for a specified time (the default is two seconds) the edit control will validate and reformat the contents,
updating the control text with the revised value.
Two additional classes are provided for demonstration purposes. TEditAngle
shows the implementation
of an edit control to accept an angle in degrees. The displayed text is formatted with a small degree symbol
following the number and is validated to be within the range 0-360. TEditDistance
demonstrates
prefix and suffix formatting text.
All the code for handling the prefix and suffix texts are handled in the abstract base class, so data exchange to
a floating point using DDX_Text(pDX, IDC_ANGLE1, m_lfAngle1);
is supported.
To implement your own validation classes, simply inherit a new class from TEditTimedUpdate
, pass in
optional text strings for the prefix and suffix text, and the update period in seconds, and overload the
AdjustValue
virtual function.
class TEditAngle : public TEditTimedUpdate
{
protected:
virtual void AdjustValue(CString &strValue);
public:
TEditAngle();
virtual ~TEditAngle();
DECLARE_MESSAGE_MAP()
};
static const unsigned char szDegrees[] = {(unsigned char)176,0};
TEditAngle::TEditAngle() : TEditTimedUpdate(NULL, (char*)szDegrees)
{
}
TEditAngle::~TEditAngle()
{
}
void TEditAngle::AdjustValue(CString &strValue)
{
double lfValue;
LPTSTR pStopString;
lfValue = _tcstod(strValue, &pStopString);
if (lfValue < 0.001)
lfValue = 0.0;
else if (lfValue > 360.0)
lfValue = 360.0;
strValue.Format("%.2f", lfValue);
}
When the value has been validated, the control sends a WM_NOTIFY
message with a
NM_EDITTIMEDUPDATE
to the parent window. The parent window should respond to this
message to perform dynamic processing instead of the usual NM_CHANGE
.
There's not too much more to say. I think this is a great class that is very simple to use and provides a very rich user interface - but then I'm probably biased ;). It also solves all the
problems will allowing non-integer numeric data entry and validates as the user enters data rather than waiting for the horrible validation messages created
by MFC's DDV_
mechanism.
Craig graduated with a B.SC. Honours in Computing for Real Time Systems from the University of the West of England, Bristol in 1995 after completing an HND in Computing in 1993.