.NET Framework Class Library
MaskedTextBox..::.Mask Property

Gets or sets the input mask to use at run time.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Property Mask As String
Visual Basic (Usage)
Dim instance As MaskedTextBox
Dim value As String

value = instance.Mask

instance.Mask = value
C#
public string Mask { get; set; }
Visual C++
public:
property String^ Mask {
    String^ get ();
    void set (String^ value);
}
JScript
public function get Mask () : String
public function set Mask (value : String)

Property Value

Type: System..::.String
A String representing the current mask. The default value is the empty string which allows any input.
Exceptions

ExceptionCondition
ArgumentException

The string supplied to the Mask property is not a valid mask. Invalid masks include masks containing non-printable characters.

Remarks

Mask is the default property for the MaskedTextBox class.

Mask must be a string composed of one or more of the masking elements, as shown in the following table. The masking language used by MaskedTextBox is defined by its associated MaskedTextProvider. The standard provider specifies a masking language based upon the one used by the Masked Edit control in Visual Basic 6.0, and should be very familiar to users migrating from that platform.

Masking 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. If the AsciiOnly property is set to true, this element behaves like the "L" element.

C

Character, optional. Any non-control character. If the AsciiOnly property is set to true, this element behaves like the "?" element.

A

Alphanumeric, optional. If the AsciiOnly property is set to true, the only characters it will accept are the ASCII letters a-z and A-Z.

a

Alphanumeric, optional. If the AsciiOnly property is set to true, the only characters it will accept are the ASCII letters a-z and A-Z.

.

Decimal placeholder. The actual display character used will be the decimal symbol appropriate to the format provider, as determined by the control's FormatProvider property.

,

Thousands placeholder. The actual display character used will be the thousands placeholder appropriate to the format provider, as determined by the control's FormatProvider property.

:

Time separator. The actual display character used will be the time symbol appropriate to the format provider, as determined by the control's FormatProvider property.

/

Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.

$

Currency symbol. The actual character displayed will be the currency symbol appropriate to the format provider, as determined by the control's FormatProvider property.

<

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 MaskedTextBox. Literals always occupy a static position in the mask at run time, and cannot be moved or deleted by the user.

If you change a mask when MaskedTextBox already contains user input filtered by a previous mask, MaskedTextBox will attempt to migrate that input into the new mask definition. If it fails, it will clear the existing input. Assigning a zero-length string as the mask will preserve any existing data in the control. When used with a zero-length mask, MaskedTextBox behaves like a single-line TextBox control.

The decimal (.), thousandths (,), time (:), date (/), and currency ($) symbols default to displaying those symbols as defined by the application's culture. You can force them to display symbols for another culture by using the FormatProvider property.

Character insertion into the mask at run time is controlled by the InsertKeyMode property. Users can navigate through the mask by using the left and right arrow keys or the mouse cursor, and can skip optional positions in the mask by entering a space.

Important noteImportant Note:

MaskedTextBox supports all Unicode characters except for surrogates and vertically combined characters.

The following table shows example masks.

Mask

Behavior

00/00/0000

A date (day, numeric month, year) in international date format. The "/" character is a logical date separator, and will appear to the user as the date separator appropriate to the application's current culture.

00->L<LL-0000

A date (day, month abbreviation, and year) in United States format in which the three-letter month abbreviation is displayed with an initial uppercase letter followed by two lowercase letters.

(999)-000-0000

United States phone number, area code optional. If users do not want to enter the optional characters, they can either enter spaces or place the mouse pointer directly at the position in the mask represented by the first 0.

$999,999.00

A currency value in the range of 0 to 999999. The currency, thousandth, and decimal characters will be replaced at run time with their culture-specific equivalents.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Community Content

Noelle Mallory - MSFT
Documentation error?

Are Mask options "A" and "a" actually identical? Shouldn't one be optional and the other required? If not, why are there two masks that have identical functions?

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Tags :

vleupold
Mask for email address --
I am trying to figure out how you might mask an email address. I would like to let the left side of the @ symbol be pretty open to input (alphanumeric) then of course the rest is obvious.

Does anyone know how to do this?

Thanks much!!

ToolmakerSteve
Re: mask for email address

MaskedTextBox is a poor choice for constraining an e-mail address, because there could be an arbitrary number of characters before and after the @ symbol. Also, some punctuation chars are accepted but others are not. A better solution is to make a normal TextBox, and give it a KeyPress event handler. Here is a "good enough" C# version that will reject most bad typing:


private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

^^TextBox tb = sender as TextBox;

^^if (tb == null)

^^^^throw new InvalidProgramException("KeyPress validator expecting a TextBox.");

^^

^^char ch = e.KeyChar;

^^int atSignIndex = tb.Text.IndexOf("@");

^^int newCharAt = tb.SelectionStart + tb.SelectionLength;
^^bool inDomain = (atSignIndex >= 0) && (newCharAt > atSignIndex);
^^
^^// "Control" chars are needed for navigating; e.g. BACKSPACE, TAB.

^^if (char.IsLetterOrDigit(ch) || char.IsControl(ch) ||
^^^^ch == '-' || ch == '_' || ch == '.')

^^^^return; // OK: accept this char.
^^else if (ch == '+' || ch == '$' || ch == '/')
^^{

^^^^// NOTE: We omitted several other chars that are rarely used.
^^^^// See http://en.wikipedia.org/wiki/E-mail_address for other chars you could list above.

^^^^if (inDomain)
^^^^^^e.Handled = true; // Reject this char after "@".
^^^^return;
^^}

^^else if (char.IsPunctuation(ch))

^^{

^^^^if (ch == '@' && atSignIndex < 0)

^^^^^^return; // OK: No "@" yet.

^^^^e.Handled = true; // Reject other punctuation.

^^}

^^else

^^^^e.Handled = true; // Reject any other char.

}


~ToolmakerSteve

Tags :

Page view tracker