See Also: MaskedTextBox Members
The System.Windows.Forms.MaskedTextBox class is an enhanced System.Windows.Forms.TextBox control that supports a declarative syntax for accepting or rejecting user input. Using the MaskedTextBox.Mask property, you can specify the following input without writing any custom validation logic in your application:
Required input characters.
Optional input characters.
The type of input expected at a given position in the mask; for example, a digit, or an alphabetic or alphanumeric character.
Mask literals, or characters that should appear directly in the System.Windows.Forms.MaskedTextBox; for example, the hyphens (-) in a phone number, or the currency symbol in a price.
Special processing for input characters; for example, to convert alphabetic characters to uppercase.
When a System.Windows.Forms.MaskedTextBox control is displayed at run time, it represents the mask as a series of prompt characters and optional literal characters. Each editable mask position, representing a required or optional input, is shown with a single prompt character. For example, the number sign (#) is often used as a placeholder for a numeric character input. You can use the MaskedTextBox.PromptChar property to specify a custom prompt character. The MaskedTextBox.HidePromptOnLeave property determines if the user sees the prompt characters when the control loses input focus.
As the user types input into the masked text box, valid input characters replace their respective prompt characters in a sequential fashion. If the user types an invalid input character, no replacement occurs, but instead a beep is issued if the MaskedTextBox.BeepOnError property is set to true, and the MaskedTextBox.MaskInputRejected event is raised. You can provide your own custom error logic by handing this event.
When the current insertion point is at a literal character, the user has a number of options:
If a character other than the prompt character is typed, the literal will automatically be skipped and the input character will be applied to the next editable position, represented by the next prompt character.
If the prompt character is typed and the MaskedTextBox.AllowPromptAsInput property is true, the input will overtype the prompt character and insertion point will be moved to the next position in the mask.
As is always the case, the arrow keys can be used to navigate to a previous or subsequent position.
You can use the MaskedTextBox.MaskFull property to verify whether or not the user has entered all of the required input. The MaskedTextBox.Text property will always retrieve the user's input formatted according to the mask and the MaskedTextBox.TextMaskFormat property.
The System.Windows.Forms.MaskedTextBox control actually defers all mask processing to the System.ComponentModel.MaskedTextProvider class specified by the MaskedTextBox.MaskedTextProvider property. This standard provider supports all Unicode characters except for surrogates and vertically combined characters; however, the MaskedTextBox.AsciiOnly property can be used to restrict input to the characters sets a-z, A-Z, and 0-9.
Masks do not necessarily guarantee that a user's input will represent a valid value for a given type; for example, -9 could be entered for an age in years. You can verify that a user's input represents a valid value by assigning an instance of that value's type to the MaskedTextBox.ValidatingType property. You can detect whether the user removes focus from System.Windows.Forms.MaskedTextBox when it contains an invalid value by monitoring for the MaskedTextBox.TypeValidationCompleted event. If type validation succeeds, the object representing the value will be available through the TypeValidationEventArgs.ReturnValue property of the System.Windows.Forms.TypeValidationEventArgs parameter.
As with the System.Windows.Forms.TextBox control, several common keyboard shortcuts do not work with System.Windows.Forms.MaskedTextBox. In particular, CTRL-R (right justify text), CTRL-L (left justify text), and CTRL-L (center text) have no effect.
System.Windows.Forms.MaskedTextBox was designed to retain most of the functionality of the Masked Edit control in vbprvb 6.0. The following table lists common properties on the Masked Edit control and gives their equivalents on System.Windows.Forms.MaskedTextBox.
AllowPrompt property | |
AutoTab property |
None |
ClipMode property | |
ClipText property |
MaskedTextBox.Text (when MaskedTextBox.TextMaskFormat is set to MaskFormat.ExcludePromptAndLiterals) |
Format property |
None |
FormattedText property |
MaskedTextBox.Text (when MaskedTextBox.TextMaskFormat is set to MaskFormat.IncludePromptAndLiterals) |
Mask property | |
PromptChar property | |
PromptInclude property | |
ValidationError event |
The System.Windows.Forms.MaskedTextBox control does not support multiline configuration or undo functionality. However, while the members associated with these features have been retained for compatibility with the System.Windows.Forms.TextBoxBase base class, their implementations perform no actions.