MaskedTextBox Control

The MaskedTextBox is an improved textbox control, supporting a declarative syntax that allows user input to be accepted or rejected. The following input can be specified using the mask property in our application without writing any custom validation logic.

  • Characters for optional input.
  • Characters are optional.
  • The type of input expected at a particular point in a mask, e.g. a digit or an alphabet character.
  • Mask literals or characters, for example, the hyphens (-), in a telephone number or the currency symbol in a price, that should directly appear in the MaskedTextBox.
  • Special input processing ; for example to convert alphabetic to uppercase characters.

 

Masked Text Box Control - UK Academe

Masked Text Box Control Run time - UK Academe

Mask is the MaskedTextBox class default property. As shown in the following table the mask must be made up of one or more masking elements.

Masking Element Description
0 Digit, necessary. Every single digit between 0 and 9 will accept this element.
9 Optional, digit or space.
# Optional, digit or space. If this position is blank in the mask, the space in the text property will be returned. Signs are allowed for both Plus (+) and Minus (-).
L Letter required. Letter required. The entry to the letters A - z and A - Z of ASCII is restricted. In regular expressions, this mask element is [ a - zA - Z ] equal.
? Letter, facultative. The ASCII letters a - z and A - Z are limited to input. Is this element of mask [ a - zA - Z ] equivalent? Regularly in terms.
& Character, necessary. This element is like the ' L ' element when the only ASCII property is set to true.
C Optional character. Any character that is not controlled. This element behaves like the? element when the ASCII Only property is true.
A Required alphanumeric. Only the characteristics of ASCII are A - z and A - Z if the Ascii property is set to true. This element mask acts as the element of "a."
a Optional, alphanumeric. The only characters it can receive are ASCII letters A - z and A - Z if the AsciiOnly property is set to true. The mask component is similar to the "A" component.
. Decimal position owner. The actual character displayed on the provisioning format is the decimal symbol, as determined by the FormatProviding property of the control.
, The placeholder is thousands. As determined by the FormatProvider control property, the actual showcase character is the one thousand placeholders for the format provider.
: Splitting Time. The current display character is the appropriate time symbol for the format provider as determined by the FormatProvider property of the control.
/ Splitting Date. The current display character will be the correct format provider date symbol, as determined by the FormatProvider property of the control.
$ Symbol for currency. A currency symbol for a format supplier, as determined by the FormatProvider control feature, will be the actual character shown.
< Shift down. Shift down. Conversion of all following characters to decrease.
> Just shift up. Shift up. Conversion to uppercase of all characters.
| Deactivate an earlier up or down shift.
\ Escapes,A mask - character escapes and makes it literal. "\\" is the backslash escape sequence.
All others Charatcters Literal, All non - mask elements are displayed in MaskedTextBox as themselves. Literals are always static in the mask at work, and the user can not move or delete them.

 

The InsertKeyMode property controls the insertion of character in the mask at runtime. Users can browse the mask using the left and right arrow keys or mouse mouse cursors and by entering an area, they can skip optional positions in the mask.

Important!  MaskedTextBox supports all Unicode characters with the exception of surrogates and combined vertically.

The table below shows examples of masks.

Mask Behavior
00/00/0000 International date format (day, number month, year). The "/" is a logical date separator and the user will be shown as a date separator that corresponds to the current culture of this application.
00->L<LL-0000 A date in the U.S. format (day, month, and year) in which an initial uppercase letter is displayed and two letters in the lowercase letter are shown.
(999)-000-0000 U.S. phone number, optional area code. If users do not want to enter the optional characters, either they can enter spaces, or directly place the mouse pointer in the first 0-character mask.
$999,999.00 The value of a currency is between 0 and 99999. At the end of the day, the currency, the thousandth, and decimal characters with their cultural equivalents will be substituted.

 

Example:

The code example below starts the MaskedTextBox to accept a date and uses the  MaskInputRejected and the TypeValidationCompleted events to warn the user of an invalid input.


private void mtxtBox_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
        {
            if(e.IsValidInput)
            {
                DateTime dateTime = (DateTime)e.ReturnValue;
                MessageBox.Show("Validated Date: " + dateTime.ToShortDateString());
            }
            else
            {
                MessageBox.Show("Invaild Date: " +mtxtBox.Text.ToString());
            }
           
        }

        private void mtxtBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Error: " + e.RejectionHint.ToString() + "position: " + e.Position.ToString());
        }

 

Video Tutorial