viksoe.dk

Edit control (filter)

Edit control (filter)


This article was submitted .


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

The control supports two kinds of filtering: an include mask and an exclude mask. Both masks will contain characters that is either not accepted or only accepted as user input. If an attempt is made to enter an illegal character, the control will play the standard beep sound.
The control cannot filter characters on particular text positions. It is only able to either exclude on include certain characters from the input stream.

Using the style setting ES_EX_JUMPY, the control can be set to automatically jump to the next control in the tab order when the user is at the very end of the input box.
For this to work, use the SetLimitText() member to limit the number of characters in each edit control.

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

CFilterEdit 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));
  ...
}

Use the following code to set up the control to only accept digits and to jump to the next control when 4 digits have been entered.
  m_ctlEdit.SetLimitText(4);
  m_ctlEdit.SetExtendedEditStyle(ES_EX_JUMPY);
  m_ctlEdit.SetIncludeMask(_T("1234567890"));

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.0 Library

See Also

My more advanced Edit validating control

Download Files

DownloadSource Code (3 Kb)

To the top