Objective Toolkit : Chapter 4 Simple Controls : Masked Edit Control
Masked Edit Control
The masked edit control implements an edit field that filters characters as they are typed. One of the primary limitations of Dynamic Data Validation (DDV) is that the user must type all the data and dismiss the dialog before the errors in input are found. The masked edit control in Objective Toolkit provides a solution to this problem by giving the user immediate feedback if an invalid character is typed.
The class hierarchy is as follows:
Figure 35 – Masked Edit Class Hierarchy
SECMaskEdit
SECMaskEdit provides a subclassed CEdit that you can use to specify the format and restrictions for entered data. For example, if you wanted a user to enter a telephone number, you would set the mask to (###) ###-####. SECMaskEdit would restrict cursor movement and data input to the location of the pound signs (#).
Figure 36 – Example SECMaskEdit Control
SECMaskEdit supports cut, copy, paste, and clear operations in addition to the insert mode, which the user can toggle on and off by pressing the VK_INSERT key.
Using SECMaskEdit
SECMaskEdit attaches to an existing CEdit control and lets you add formatted input capabilities to it.
To incorporate an SECMaskEdit object into your code:
Attach an SECMaskEdit object to an existing CEdit using the AttachEdit() method.
To set a mask that defines the format for the data the user enters:
Construct a string containing the special mask codes and pass it to the control with the SetMask() method. For more information, see “Creating a Mask to Use with SECMaskEdit”.
To retrieve the data from the masked edit control:
Call GetData() to retrieve the data without the mask or GetRawData() to retrieve the entire contents of the SECMaskEdit.
To use DDX (dynamic data exchange) with an SECMaskEdit:
Use the DDX_Mask() function instead of DDX_Control() function in your dialog’s DoDataExchange() method.
Creating a Mask to Use with SECMaskEdit
Mask strings consist of mask characters and literals. Literals are characters that appear unchanged in the mask. Mask characters specify a spot in the mask that accepts certain characters. The valid mask characters are as follows:
Table 20 – Mask Characters for SECMaskEdit 
Mask Character
Input allowed for that field
#
Numeric data (0-9)
A
Alpha-numeric data (0-9 and a-Z)
&
Any ASCII character
?
Alphabetic data (a-Z)
U
Accepts a-Z, forces to A-Z (uppercase)
L
Accepts a-Z, forces to a-z (lowercase)
\
Escape character. Use this character if you want the application to interpret one of the preceding character literally (in other words, shown in the mask).
 
NOTE >> The ES_LOWERCASE and ES_UPPERCASE styles have precedence over the ‘U’ and ‘L’ masks.
Here are some of more popular masks:
Table 21 – Popular Masks for SECMaskEdit 
Description
Mask
Example
Date
##/##/##
12/24/98
Time
##:## UU
12:35 AM
Soc. Sec. Number
###-##-####
148-92-1532
Phone
(###) ###-#### [####]
(919) 933-0867 [7]
Zip code + 4
#####-####
27858-1203
First Name
????????????????
Bartholomew
 
At run time, a prompt character replaces the mask characters. By default, the prompt character is a space. You can change the prompt character via the SetPromptChar() method, e.g., in the case that the space is used as a valid input character.
NOTE >> In this version of SECMaskEdit, validation is not performed on the various types of data. For example, 12/35/95 is invalid for a US date format, but would fit the requirements of the mask. It is the SECMaskEdit user’s task to detect that 12/35/95 is an invalid date in such a circumstance.
Mask Edit Sample
The Objective Toolkit masktest sample in the Samples\Toolkit\MFC\Controls\masktest directory demonstrates the creation and capabilities of SECMaskEdit, including DDX (dynamic data exchange). This sample is not shipped with the product. For information on how to obtain this sample, see “Location of Sample Code” in the Getting Started part.