Environment: VC6 SP5 Introduction 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 […]
CodeGuru content and product recommendations are
editorially independent. We may make money when you click on links
to our partners.
Learn More

Environment: VC6 SP5
Introduction
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
{
public:
void SetPrompt(char prompt);
void ConverToUpper(bool upper = true) { bUpper = upper;
bLower = !upper; };
void ConverToLower(bool lower = true) { bLower = lower;
bUpper = !lower; };
void CopyNonMasked(bool copyall = true) { bCopyAll = copyall; };
void SetPasswordChar(TCHAR ch);
TCHAR GetPasswordChar() { return chPassword; };
void SetMask(CString mask);
CString GetData(int start = 0, int len = -1);
void SetData(CString data);
}
Downloads
Download demo project – 16 Kb
Download source – 7 Kb