viksoe.dk - Edit control (pattern matching) (original) (raw)

This is an EDIT control, which filters the user's input.

The control uses a mask to make sure that the user enters data in the required format. A pattern-matching algorithm enforces the mask.

In addition, the control supports a number of different styles to provide visual cues for error indication and handling.
All styles are set using the SetExtendedEditStyle()method. It is also possible to change various colors using SetTextColor() and SetCompleteColor(). Set the pattern using SetPattern().
When using a fixed length pattern you can use the SetLimitText() member to limit the number of characters in the edit control.

Pattern Matching

The pattern-matching algorithm supports the following tokens:

? = match single [abc] = match set [a-z] = match range [^a-z] = exclude range \x = escape

Though using Regular Expressions would have provided greater flexibility, this pattern algorithm is quite good for simple masks.

The default pattern is a * (match all) mask.

Visual cues

The control offers a range of visual (and audio) cues to signal problems in the input. One or more cues can be enabled.

Error Beep

When the user attempts to enter a text, which doesn't comply with the pattern mask, the control will play the standard beep sound and refuse to change the text.

Red marks

The control displays the offending part of the text in red color to show the user where the mistake was made.

Red stripes

The control shows red wavy underlines to indicate input errors in the text. The underlines start at the offending character position.
This is the effect known from Microsoft Word's spell checker.
It works only if you allow a few additional pixels to the EDIT control's height.

Tool Tip

In the event of an error, a tooltip can be shown above the edit control and in a distinctive red color. The tool tip text must be supplied by the owner window by supporting the TTN_GETDISPINFO tool tip notification. The owner can use methods such as GetParseState() andGetWindowText() to deduce what is wrong with the input to provide different tooltip texts in different situations.
The color of the tip background and text can be changed.

Success coloring

You can set a different the text color when the input text matches the pattern. This way the user will know when the input text is accepted.

How to use it

To use this WTL control, place an Edit control on your dialog.
Add a member variable to your dialog implementation file...

CValidateEdit m_ctlEdit

In the OnInitDialog() event handler, add the following line:

LRESULT OnInitDialog(UINT /*uMsg*/, 
                     WPARAM /*wParam*/, 
                     LPARAM /*lParam*/, 
                     BOOL& /*bHandled*/)
{
  ...
  m_ctlEdit.SubclassWindow(GetDlgItem(IDC_EDIT1));
  ...
}

Add the following reflection macro to your main message map:

BEGIN_MSG_MAP(CMainDlg)
  ...
  REFLECT_NOTIFICATIONS()
END_MSG_MAP()

Use the following initialisation code to set the control to display wavy underlines to indicate errors in the input text. The pattern allows text like 1G0b and 2G22.

  m_ctlEdit.SetPattern(_T("[1-2]G[023][0-9abc]"));
  m_ctlEdit.SetLimitText(4);
  m_ctlEdit.SetExtendedEditStyle(ES_EX_ERRORSTRIPES);
  m_ctlEdit.SetCompleteColor(RGB(0,128,0));

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.0 Library

See Also

A more simple filtering Edit control

Download Files