WinUI コントロール
MaskedTextBox
コントロール > Input > MaskedTextBox

MaskedTextBoxMaskedTextBox is an advanced textbox control which automatically validates input by a user. It exists as a completely function text box which includes a basic text input area and can be further customized with a mask. The desired mask can be set by using Mask property of the MaskedTextBox class.

Masked Textbox

Mask Types

You can apply different types of masks to the MaskedTextBox control by specifying the following mask characters to the Mask property. Once the mask is applied, the input entered by a user is validated and invalid characters is not allowed in the control.

Placeholder Option Description
0 Digit, required. This element will accept any single digit between 0 and 9.
9 Digit or space, optional.
# Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the?Text?property. Plus (+) and minus (-) signs are allowed.
L Letter, required. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z] in regular expressions.
? Letter, optional. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z]? in regular expressions.
& Character, required.
A Alphanumeric, optional.
a Alphanumeric, optional.
. Decimal placeholder. The actual display character used will be the decimal symbol appropriate to the format provider.
, Thousands placeholder. The actual display character used will be the thousands placeholder appropriate to the format provider.
: Time separator. The actual display character used will be the time symbol appropriate to the format provider.
/ Date separator. The actual display character used will be the date symbol appropriate to the format provider.
All other characters Literals. All non-mask elements will appear as themselves within?C1MaskedTextBox. Literals always occupy a static position in the mask at run time and cannot be moved or deleted by the user.

In addition to the mask characters, you can choose to include prompt characters in the MaskedTextBox using the PromptChar property. These characters appears in the control to show spaces where the user is supposed to enter text.

To add mask characters and prompt characters in the MaskedTextBox control, use the following code:

C#
コードのコピー
maskedTextBox.Mask = "000-000";
maskedTextBox.PromptChar = '#';