Click here to Skip to main content
Click here to Skip to main content

Timed Update Edit Control

, 9 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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 Wink | ;) . 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)

Share

About the Author

Craig Henderson
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.
Follow on   Twitter

Comments and Discussions

 
QuestionThis is a nice control [modified] PinmemberFlaviu210-Sep-13 3:25 
NewsThis is an OLD article PinprofessionalCraig Henderson9-Sep-13 0:10 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 9 Sep 2013
Article Copyright 2000 by Craig Henderson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid