System.Windows.Forms.MaskedTextBox Class

Uses a mask to distinguish between proper and improper user input.

See Also: MaskedTextBox Members

Syntax

[System.ComponentModel.DefaultBindingProperty("Text")]
[System.ComponentModel.Designer("System.Windows.Forms.Design.MaskedTextBoxDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.DefaultEvent("MaskInputRejected")]
[System.ComponentModel.DefaultProperty("Mask")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
public class MaskedTextBox : TextBoxBase

Remarks

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:

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:

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.

Compatibility with Visual Basic 6.0

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

MaskedTextBox.AllowPromptAsInput

AutoTab property

None

ClipMode property

MaskedTextBox.CutCopyMaskFormat

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

MaskedTextBox.Mask

PromptChar property

MaskedTextBox.PromptChar

PromptInclude property

MaskedTextBox.ResetOnPrompt

ValidationError event

MaskedTextBox.MaskInputRejected

Note:

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.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0