Character-Based Masked Edit

Introduction

Most masked edit controls allow you to set the mask of the entire edit control, making each character have the same mask. Sometimes this is ok, but sometimes it is not. For example, you may have a 6-digit part number, where the first digit can only be the letters A or B, the second digit can only be a vowel, and the remaining digits can be 0 through 9 inclusive. Try doing that with a conventional masked edit control.

With my masked edit control, you will be able to set the mask for each character in an edit control. Although I've totally written this control from scratch, I got the idea from a former (and very brilliant) co-worker of mine, Dave Kelly.

How to construct the mask

To set the mask, you will need to call the Init() function and pass it a mask and optional initial data. The mask is constructed so that each character is inside the brackets, and all static characters (characters not enterable by the user, such as a dash or a slash) are outside the brackets. Dashes can be used inside the brackets to indicate a range of characters (based on the ASCII value of the start and end character).

Example masks

The part number mentioned above in the Introduction: "[AB][AEIOU][0-9][0-9][0-9][0-9]"
Telephone number in the 719 area code: "(719) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
A 2-digit, alphanumeric, not case-sensitive: "[A-Za-z0-9][A-Za-z0-9]"

The demo project is a test app in which you can enter a mask, or you can select a pre-defined mask from the combobox. Click the "Set Mask" buttons to set the mask of the edit control that is next to it.

Data exchange

To get the data out of the control, call the function GetData(). This function will return a string of characters that are enterable by the user (not static characters).

To set the data in the control, call the function SetData(), passing it a CString of the data you want prefilled.

You can always call GetWindowText() to get the data WITH the static characters in it. SetWindowText() can be called as well, but be careful that your string parameter will not conflict with your mask.

There is a DDX_CmEdit() function as well, so you can associate an edit control with a CString. Just call UpdateData() to do the data exchange.

Prompt character

The default prompt character is set to be an underscore. However, you can change this by calling the SetPromptChar() function.

Downloads

Download demo project - 23 KB
Download source - 5 KB



Comments

  • Great work!

    Posted by Legacy on 09/06/2001 12:00am

    Originally posted by: Jesper Smedegaard

    Just wanted to let you know. I�ve been looking for a good and flexible CEdit control. This is it!

    Thanks.

    Reply
  • Locking the control?

    Posted by Legacy on 01/05/2001 12:00am

    Originally posted by: Craig

    I have found your control very useful, thanks.

    The only problem I have found is I cannot lock the control during runtime. Does anyone know how to do this?

    Reply
  • Great - looking forward to V2

    Posted by Legacy on 12/07/1999 12:00am

    Originally posted by: John

    Hi,

    This works really well !

    I have a wishlist for the next release :
    (1) Allow for certain characters to be optional or mandatory - currently the user can skip past characters without typing anything.

    (2) Provide visual feedback to the user as to the character(s) allowed/expected in the edit control.

    Keep up the good work. If I manage to implement any of the above myself, I'll post the code.

    Reply
  • CPicEdit ...

    Posted by Legacy on 12/06/1999 12:00am

    Originally posted by: viklele

    You might like to compare your implementation with CPicEdit. Its a sample available (or rather used to be available) on MSDN. I tried looking for it on microsoft site, its not available now. You might find it in one of the old MSDN CDs (1996/97 CDs).

    Your implementation has a significant improvement over CPicEdit that you are using regular expression like mask... allowing you to specify any set of characters for each entry position. As against this CPiCEdit has pre-defined mask sets. Still you might like to look it up ... as its more complete in implementation.

    - Vikram

    Reply
  • Minor glitches...

    Posted by Legacy on 12/04/1999 12:00am

    Originally posted by: Andrew

    First, the entire contents of the textbox becomes selected when the app gets activated if the control has focus. Second, you cannot select text using shift and the arrow keys - only using the mouse or things like [Shift][Home] or [Shift][End].

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

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds