The Text and ClipText Properties
All data entered in the MaskedEdit control is contained in and can be retrieved from the Text property. This is a run time only property and includes all the literal and prompt characters of the input mask. For instance, retrieving data from the Text property of the example above returns the string "(555) - 555 - 5555" – the phone number that was entered.
The ClipText property also returns data entered in the MaskedEdit control, but without the literal and prompt characters. Using the example above, retrieving data from the ClipText property returns the string "5555555555". The ClipText property is available only at run time.
Defining the Input Character
By default, all mask characters are underlined. This indicates to the user that the character is a placeholder for data input. When the user enters a valid character, the underline disappears. If you want the underline to remain, you can set the FontUnderline property of the MaskedEdit control to True.
You can also change the underline input character to a different character by using the PromptChar property. For example, to change the underline (_) character to the asterisk (*) character, you simply redefine the value of the PromptChar property:
MaskEdBox1.PromptChar = "*"
Using Mask Characters as Literals
If you want to use a mask character as a literal, you can precede the mask character with a backslash (\). For example, if you want the pound sign (#) to display, you set the mask as follows:
MaskEdBox1.Mask = "\##"
This would produce a mask that displays a pound sign (#) followed by a blank space for entering a number.
The Format Property
You can modify how the MaskedEdit control is displayed and printed using the Format property. The Format property provides you with standard formats for displaying number, currency, and date/time information.
The following table lists the standard formats you can use with the Format property:
Data type Value Description
Number (Default) Empty string General Numeric format. Displays as entered.
Number $#,##0.00;($#,##0.00) Currency format. Uses thousands separator; displays negative numbers enclosed in parentheses.
Number 0 Fixed number format. Displays at least one digit.
Number #,##0 Commas format. Uses commas as thousands separator.
Number 0% Percent format. Multiplies value by 100 and appends a percent sign.
Number 0.00E+00 Scientific format. Uses standard scientific notation.
Date/Time (Default) c General Date and Time format. Displays date, time, or both.
Date/Time dddddd Long Date format. Same as the Long Date setting in the International section of the Microsoft Windows Control Panel. Example: Tuesday, May 26, 1992.
Date/Time dd-mmm-yy Medium Date format. Example: 26-May-92.
Date/Time ddddd Short Date format. Same as the Short Date setting in the International section of the Microsoft Windows Control Panel. Example: 5/26/92.
Date/Time ttttt Long Time format. Same as the Time setting in the International section of the Microsoft Windows Control Panel. Example: 05:36:17 A.M.
Date/Time hh:mm A.M./P.M. Medium Time format. Example: 05:36 A.M.
Date/Time hh:mm Short Time format. Example: 05:36.
You use the Format property with the Mask property. For example, to create a mask that prompts for a Short Date input that displays in the Long Date format, you set the Mask and Format properties as follows:
MaskEdBox1.Mask = "##-##-##"
MaskEdBox1.Format = "dddddd"
When the user enters the date in the short format (06-27-96, for instance), the MaskedEdit control verifies that the entered data is valid, and then, when the focus passes to the next control, it is displayed as "Thursday, June 27, 1996".
Note To automatically shift the focus to the next control when the data has been verified as valid, set the AutoTab property of the MaskedEdit control to True.
The Format property also allows you to specify custom formatting using the same format expressions defined by the Visual Basic Format function.
For More Information See "Format Function" or "Format Property (MaskedEdit control)."
Setting Properties at Design Time
You can set the property values at design time using the MaskedEdit control Property Pages. Click the Custom option in the Properties window of the MaskedEdit control to bring up the Property Pages dialog box, as shown below:
Setting the Mask property at design time
You enter the mask and format patterns as in the run time examples above. The Format drop down list allows you to select any of the predefined standard formats shown above. This dialog box also allows you to easily set such properties as PromptChar, ClipMode, and MaxLength.
A MaskedEdit field can have a maximum of 64 characters (the valid range is 1 to 64 characters). This includes literal characters as well as mask characters. You can set this value using the MaxLength property. At design time, this property is set automatically to the number of characters in the pattern when you enter a mask pattern.
The ClipMode property specifies whether or not literal characters are included when doing a cut or copy command. By default, when a selection in the MaskedEdit control is copied to the Clipboard, the entire selection, including the literals, is transferred. To limit the copy operation to only the data entered by the user, set the ClipMode property to True.
The ValidationError Event
The ValidationError event occurs when the MaskedEdit control receives invalid input, as determined by the input mask. For example, if you've defined an input mask that prompts for numbers, a ValidationError event will occur if the user attempts to enter a letter. Unless you write an event handler to respond to the ValidationError event, the MaskedEdit control will simply remain at the current insertion point — nothing will happen.
Mask characters are validated as they are entered and the insertion point is shifted to the right. When a character is entered or deleted out of sequence (when a digit is inserted or deleted after the phone number has been entered, for example), all nonliteral characters shift either to the right or left. When the shift occurs, if an invalid character replaces the position of a valid character, the ValidationError event 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" before 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 MaskedEdit control also validates the values of the Text property at run time. If the Text property settings conflict with the input mask, the control generates a run-time error.
You can select text in the same way you would with 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 event is generated.
For More Information See "ValidationError Event."
Using MaskedEdit as a Bound Control
The MaskedEdit control is a bound control. This means that it can be linked to a data control and display field values for the current record in a data set. The MaskedEdit control can also write out values to a data set.
Note 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.
The MaskedEdit control has three bound properties: DataChanged, DataField, and DataSource.
For More Information See "Using the ADO Data Control" in "Using Visual Basic's Standard Controls" in the Programmer's Guide for information on using bound controls.