A Validating Edit Control

Display Change

In order to indicate the state and provide immediate feedback to the user, I modify the background color of the control. I selected white for an empty control, red for an invalid value, yellow for a value that is syntactically correct so far but is not yet completely valid, and green for values that meet all criteria. These are illustrated below.

The edit control is empty. It displays as the normal edit background, which on this machine is white.
The edit control has a syntactically valid value that matches any range constraints (for this example, range checks are not enabled).
The edit control has a value that has not yet been completed. It is not yet syntactically valid, but what is there so far is correct.
The edit control has a value that is not syntactically correct. No amount of addition to this value can make it correct.

Control Update

I need to update the controls in response to changes in the validation. This means that I need to enable or disable controls (such as the OK button) whenever the state changes. In order to do this, I send a notification to the parent window indicating that a change in the validation status has occurred. This is another Registered Window Message.

/****************************************************************************
*                                UWM_VALID_CHANGE
* Inputs:
*        WPARAM: (WPARAM)MAKELONG(GetDlgCtrlID(), BOOL) Flag indicating new valid state
*        LPARAM: Window handle
* Result: LRESULT
*        Logically void, 0, always
* Effect: 
*        Notifies the parent that the validity of the input value has changed
****************************************************************************/

When I get this message I invoke the following handler

LRESULT CValidatorDlg::OnValidChange(WPARAM, LPARAM lParam)
   {
    CWnd::FromHandle((HWND)lParam)->InvalidateRect(NULL);
    updateControls();

    return 0;
   } // CValidatorDlg::OnValidChange

Note the initial InvalidateRect. I have to force a redraw of the entire control when the valid state changes, else the display ends up slightly skewed, with only the background behind the letters being redrawn. As you read the code you will find several other full-control invalidations that guarantee the correct appearance is maintained. These are necessary because Windows is actually very good at minimizing the redrawing of the control, and if you type the value as shown below without this invalidation, the results are strange indeed. You may even note that some of the values which, if typed left-to-right, should have produced valid values appear in red indicating invalid values. This is because the InvalidateRect had not been done at all. Note also how parts of the background which are outside the character cells is invalid.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”