Visual Basic: MaskedEdit Control

Masked Edit Control

See Also   Example   Properties   Methods   Events

The Masked Edit control provides restricted data input as well as formatted data output. This control supplies visual cues about the type of data being entered or displayed. This is what the control looks like as an icon in the Toolbox:

File Name

MSMASK32.OCX

Class Name

MaskEdBox

Remarks

The Masked Edit control generally behaves as a standard text box control with enhancements for optional masked input and formatted output. If you don't use an input mask, the Masked Edit control behaves much like a standard text box, except for its dynamic data exchange (DDE) capability.

If you define an input mask using the Mask property, 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).

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

The Masked Edit control has three bound properties: DataChanged, DataField, and DataSource. This means that it can be linked to a data control and display field values for the current record in the recordset. The Masked Edit control can also write out values to the recordset.

When the value of the field referenced by the DataField property is read, it is converted to a Text property string, if possible. If the recordset is updatable, the string is converted to the data type of the field.

To clear the Text property when you have a mask defined, you first need to set the Mask property to an empty string, and then the Text property to an empty string:

MaskedEdit1.Mask = ""
MaskedEdit1.Text = ""

When you define an input mask, the Masked Edit control behaves differently from the standard text box. 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 event is triggered.

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 event.

The Masked Edit control also validates the values of the Text property at run time. If you set the Text property so that it conflicts with the input mask, the control generates a run-time error.

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 left of the selection.

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 ClipMode property to transfer only user-entered data onto the Clipboard literal characters that are part of the input mask are not copied.