COXMaskedEdit Overview

Dundas

COXMaskedEdit Overview

Copyright © Dundas Software Ltd. 1997 1999, All Rights Reserved

Class Members | Mask Characters

The COXMaskedEdit control extends the MFC CEdit control to provide restricted data input with visual cues, formatted data output, overtype capability, and a validation framework.

You can use COXMaskedEdit anywhere you would use a CEdit class. If no input mask is set, it will behave like a standard CEdit.

If you define an input mask, each character position in the Masked Edit control maps to either a placeholder of a specified type or a literal character. (Literal characters, or 'literals', can give visual cues about the type of data being used. For example, the parentheses surrounding the area code of a telephone number are literals: (206) 777-2222.)

The input mask prevents you from entering invalid characters into the control. If you attempt to enter a character that conflicts with the input mask, the control generates a ValidationError beep.

The insertion point automatically skips over literals as you enter data or move the insertion point.

When you insert or delete a character, all nonliteral characters to the right of the insertion point are shifted, as necessary. If shifting these characters leads to a validation error, the insertion or deletion is prevented, and a ValidationError beep is triggered.

For example, suppose the Mask property is defined as "?###", and the current value of the Text property is "A12." If you attempt to insert the letter "B" to the left of the letter "A," the "A" would shift to the right. Since the second value of the input mask requires a number, the letter "A" would cause the control to generate a ValidationError beep.

The Masked Edit control also validates the parameter value of the SetInputText function the user passes at run time. If you use the SetInputText function so that it conflicts with the input mask, the function will return an errorcode.

You may select text in the same way as for a standard text box control. When selected text is deleted, the control attempts to shift the remaining characters to the right of the selection. However, any remaining character that might cause a validation error during this shift is deleted, and no ValidationError beep is generated.

Normally, when a selection in the Masked Edit control is copied onto the Clipboard, the entire selection, including literals, is transferred onto the Clipboard. You can use the SetClipMode function to define the behavior for transferring only user-entered data onto the Clipboard or not - literal characters that are part of the input mask are not copied.

Using COXMaskedEdit

You can attach a COXMaskedEdit to an existing edit control by subclassing the latter.

This is remarkably simple to do in the DevStudio IDE when working with an MFC dialog.

Place a standard edit control on the dialog using the dialog editor. Invoke the Class Wizard and select the Member Variables page. Add a member variable for the ID of the edit control, selecting a CEdit control as the type.

Next, open the header file for the dialog. Include OXMaskedEdit.h.

In the AFX_DATA section for the dialog you will see the declaration for the edit control as a CEdit. Change this to COXMaskedEdit (or a class derived from COXMaskedEdit) and viola!

Typically you will call SetMask and SetPromptSymbol on the control in OnInitDialog to set up your particular mask etc.

Note that you won't need to call Create in this scenario.

Don't forget to include the OXMaskedEdit.cpp file in your project!

Depending on the order of compilation you may also find if helpful to include OXMaskedEdit.h in the dialog and/or main application .cpp file of your project.