Masked CEdit Control

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:
  // 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);
}

Downloads

Download demo project - 16 Kb
Download source - 7 Kb


Comments

  • Can mEdit receive arbitrary length strings?

    Posted by Legacy on 12/15/2003 12:00am

    Originally posted by: mikadi

    Hi.
    Cool control! But what if I need to receive strings of arbitrary length, or unmasked input (just like ordinary editbox)? Can I adjust mEdit to behave like ordinary unmasked editbox without having to manually replace mEdit with CEdit in runtime?

    Reply
  • Useful class.... But some one already done this Edit template

    Posted by Legacy on 08/13/2003 12:00am

    Originally posted by: Imran

    While ago some one published this article marely on a program which deonstrated edit template on the fly on the  codeguru site. This technique much better way but needed lot of coding. This time we got a ready made Edit Template CLASS!
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds