Masked CEdit Control

Environment: VC6 SP5


CmEdit is a class that allows users to have a masked edit control based off of CEdit. Unlike most masked edit controls, it allows you to specify a mask for each character in the box. It also has the ability to be a masked password edit box, meaning it will be a masked edit that displays *’s in place of what is typed.

This control also attempts to trap most messages such as Cut, Copy, Paste, Clear, and so forth, and handle them similarly to how a normal CEdit control would. The movement keys and Backspace/Delete also work how you would expect them to.

To use this class in your project, make a CEdit object, change it to CmEdit, and #include “mEdit.h”. Make sure you set the mask by calling the CmEdit::SetMask(CString) function. For each character you want to allow input for, list all the characters you wish to allow inside of brackets []. You may list a range of allowed characters by using a dash example; a-z will allow all characters a through z. If you want to include a ] or – in your mask (in other words, allow them to enter those characters), precede them with a \\. For example, to allow them to enter a -, use a mask of [\\-]. Any characters in the mask that are not inside brackets are considered static and will always be displayed and the user will not be able to edit them.

Some Notes on this Class

  • Pasting text into the box or using SetWindowText to set the text will simulate the user typing that text into the box. This means it will start at the current cursor location and pretend to type each character into the box.
  • You can set what character is used as a prompt by using CmEdit::SetPrompt(char).
  • To turn the control into a password masked edit, call CmEdit::SetPasswordChar(char). To turn it back into a un-passworded masked edit box, call CmEdit::SetPasswordChar(NULL).
  • If you want Copy(), Cut(), or GetWindowText() to return the static (always displayed, non-masked) characters from the box, call CmEdit::CopyNonMasked(true)
  • CmEdit::ConvertToUpper(true) will convert any allowed lowercase characters into uppercase. If your mask does not allow typing in uppercase characters, the box will only accept lowercase as inputs but will convert them to upper!
  • CmEdit::ConvertToLower(true) is the same but converts to lowercase characters.
  • The Enter key moves to the next ‘field’ in the mask and if it’s at the last field will attempt to move to the next control in the dialog.
  • PGUP and PGDOWN move to the previous/next fields in the box.

Here are some of the public functions you can use with this class:

class CmEdit : public CEdit
// sets the character used to indicate a prompt (a masked char
// w/o any current input)

void SetPrompt(char prompt);

// if the mask allows lowercase characters, converts them to
// uppercase
// this does not implicitly allow uppercase characters to be
// typed in!

void ConverToUpper(bool upper = true) { bUpper = upper;
bLower = !upper; };
// if the mask allows uppercase characters, converts them
// to lowercase
// this does not implicitly allow lowercase characters to be
// typed in!

void ConverToLower(bool lower = true) { bLower = lower;
bUpper = !lower; };

// indicates that GetData() should return the static
// characters, too
// the class defaults to false

void CopyNonMasked(bool copyall = true) { bCopyAll = copyall; };

// sets the character to use for passworded text
void SetPasswordChar(TCHAR ch);
// returns the current password character
TCHAR GetPasswordChar() { return chPassword; };

// sets the mask to use
// masked chars are listed between [] brackets
// character ranges can be indicated using a -, example a-z will
// allow all chars a to z
// if you wish to include ] or – in your allowed mask,
// preceed that char with \\, for example \\] or \\-

void SetMask(CString mask);

// returns the contents of the box from start for len chars
CString GetData(int start = 0, int len = -1);

// acts as if the user had typed this string into the box,
// starting at char 0

void SetData(CString data);


Download demo project – 16 Kb

Download source – 7 Kb

More by Author

Must Read