Click here to Skip to main content
Licence CPOL
First Posted 17 Aug 2000
Views 42,225
Bookmarked 16 times

Timed Update Edit Control

By | 17 Aug 2000 | Article
An edit control to periodically validate and reformat it's contents
  • Download source files - 6 Kb
  • Download demo project - 5 Kb
  • 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 ;). 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)

    About the Author

    Craig Henderson

    Other
    Centrix Software
    United Kingdom United Kingdom

    Member

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

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    -- There are no messages in this forum --
    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.5.120517.1 | Last Updated 18 Aug 2000
    Article Copyright 2000 by Craig Henderson
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid