ComponentOne Input Library for WPF
Input Library Overview / Masked Text Box / Mask Elements
In This Topic
    Mask Elements
    In This Topic

    MaskedTextBox for WPF supports the standard number formatting strings defined by Microsoft. The Mask string should consist of one or more of the masking elements as detailed in the following table:

    Element 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.
    C Character, optional. Any non-control character.
    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.
    $ Currency symbol. The actual character displayed will be the currency symbol appropriate to the format provider.
    < Shift down. Converts all characters that follow to lowercase.
    > Shift up. Converts all characters that follow to uppercase.
    | Disable a previous shift up or shift down.
    \ Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.
    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.

    The decimal (.), thousandths (,), time (:), date (/), and currency ($) symbols default to displaying those symbols as defined by the application's culture.

    Literals

    In addition to the mask elements already defined, other characters can be included in the mask. These characters are literals. Literals are non-mask elements that 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.

    For example, if the Mask property has been set to "(999)-000-0000" to define a phone number, the mask characters include the "9" and "0" elements. The remaining characters, the dashes and parentheses, are literals. These characters will appear as they are in the C1MaskedTextBox control.

    Note: The TextMaskFormat property must be set to IncludeLiterals or IncludePromptAndLiterals for literals to be used. If you do not want literals to be used, set TextMaskFormat to IncludePrompt or ExcludePromptAndLiterals.

    Delete Characters in a MaskedTextBox Control

    Normally, you can use the backspace key to delete characters from the right end of a masked textbox, regardless of their type. However, when you try to delete characters from a specific spot within the textbox, whether by backspace or delete, the masked textbox may stop you from deleting beyond a certain point. This occurs because when a character is deleted from the middle, the remaining characters shift left. If the characters are of different element types, the textbox does not allow this shift, preventing further deletion.

    For example, consider the Mask=” (999)-000-0000”. In this masking, the element type of all the characters is same, that is, digit. So, the deletion can take place easily from any position in the masked textbox, that is, from extreme right, left or middle of characters.

    Consider another example where the mask is set to "LL:####:00.A###". If you enter the value "AB:1234:56.7890" into the masked textbox and attempt to delete one of the letters, such as 'A' or 'B', the textbox will prevent this action. This restriction occurs because, upon deletion, the number '1' would need to shift left to occupy the position of the deleted letter. However, due to the masking rules, that position is reserved exclusively for letters (A-Z), so the masked textbox blocks the deletion to maintain the input's format.

    In conclusion, the masked textbox restricts the deletion of characters whenever a character would need to move into a position reserved for a different element type. This ensures that the integrity of the input format is maintained, preventing characters from occupying positions meant for other types.

    See Also