Click here to Skip to main content
15,881,089 members
Articles / Desktop Programming / MFC

Timed Update Edit Control

Rate me:
Please Sign up or sign in to vote.
4.72/5 (8 votes)
9 Sep 2013CPOL 65K   1.9K   27   3
An edit control to periodically validate and reformat its contents.

Sample Image - TimedUpdateEdit.gif

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.

C++
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United Kingdom United Kingdom
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.

Comments and Discussions

 
QuestionGreat control Pin
Michael Haephrati7-Jan-15 12:05
professionalMichael Haephrati7-Jan-15 12:05 
QuestionThis is a nice control Pin
_Flaviu10-Sep-13 2:25
_Flaviu10-Sep-13 2:25 
NewsThis is an OLD article Pin
Craig Henderson8-Sep-13 23:10
Craig Henderson8-Sep-13 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.