Enhanced Focus Edit Control with Input Filtering






4.17/5 (6 votes)
An edit control that provides strong visual feedback when it has the input focus, and allows filtering of input and/or displayed characters.
This article describes a class (CFocusEditCtrl
) for an
Enhanced Focus Edit Control with filtering capability. This control serves
two purposes. First, it provides visual feedback when it has focus by
changing it's background color. Second, it supports the filtering of
input.
The visual feedback is useful when the control is used on a crowded interface, and redesign of the interface is not possible. On crowded interfaces, it is often easy to "lose" the caret, and the user is unable to tell which edit control (if any) has the input focus. The changed background color solves that problem.
The filtering is useful for when you need a control that accepts only numeric values for input. A numeric-style edit control does not work correctly here, because it does not allow characters like "+", "-" and "." (decimal point). This control allows you to specify either a set of valid characters OR a set of invalid characters. The filtering also works on a Paste operation.
It is important to mention that this control only provides validation on a character-by-character basis. That means that while it can prevent characters that are not used to specify numeric values, it cannot enforce the proper format of said value. For example, limiting to numeric value characters does not prevent a string like "123.456.7890" from getting entered (even though it is technically an invalid numeric value).
Functions of interest are:
void EnableFileDropping( bool bEnable = true )
void SetExcludeMask( LPCTSTR cpMask = NULL )
void SetIncludeMask( LPCTSTR cpMask = NULL )
void StripFilteredChars( LPTSTR cpBuffer )
The EnableFileDropping(...)
function is used to
allow the edit control to accept a draggeg item, and allow it to be
dropped. This can be useful for getting a file path.
The SetExcludeMask(...)
function allows you
to specify a set of characters that are not to be
allowed into the control. For example, an exclude mask of
"1234567890" prevents numeric data from being entered into the
control.
The SetIncludeMask(...)
function
allows you to specify a set of characters that are
allowed to be in the control. For example, an include mask of
"1234567890-+." allows only the entry of numeric data.
The StripFilteredChars(...)
function allows
you remove "bad" characters from the passed string. Do this
before calling SetWindowText(...)
with any string.
In the demo image above, the edit control that has the input focus is clearly indicated, and the other two controls are setup to allow only certain data. Running the app is more useful that my talking about it! :)
Conditions of use are detailed in the header and source file(s).
Feel free to contact me if there are any questions. Have fun.