C# | MaskedTextBox Class
In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input. The MaskedTextBox class is used to represent the windows masked text box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.
This class enhanced version of TextBox control, it supports a declarative syntax for receiving or rejecting the user input and when this control display at run time, it represents the mask as a sequence of prompt characters and optional literal characters. In C# you can create a MaskedTextBox in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a MaskedTextBox as shown in the following steps:
- Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

- Step 2: Next, drag and drop the MaskedTextBox control from the toolbox to the form.

- Step 3: After drag and drop you will go to the properties of the MaskedTextBox control to modify MaskedTextBox according to your requirement.

Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a MaskedTextBox control programmatically with the help of syntax provided by the MaskedTextBox class. The following steps show how to set the create MaskedTextBox dynamically:
- Step 1: Create a MaskedTextBox control using the MaskedTextBox() constructor is provided by the MaskedTextBox class.
// Creating a MaskedTextBox control MaskedTextBox mbox = new MaskedTextBox();
- Step 2: After creating MaskedTextBox control, set the property of the MaskedTextBox control provided by the MaskedTextBox class.
// Setting the properties // of MaskedTextBox mbox.Location = new Point(374, 137); mbox.Mask = "000000000"; mbox.Size = new Size(176, 20); mbox.Name = "MyBox"; mbox.Font = new Font("Bauhaus 93", 18); - Step 3: And last add this MaskedTextBox control to the form using the following statement:
// Adding MaskedTextBox // control on the form this.Controls.Add(mbox);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp36 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the// properties of the LabelLabel l1 =newLabel();l1.Location =newPoint(413, 98);l1.Size =newSize(176, 20);l1.Text =" Example";l1.Font =newFont("Bauhaus 93", 12);// Adding label on the formthis.Controls.Add(l1);// Creating and setting the// properties of the LabelLabel l2 =newLabel();l2.Location =newPoint(242, 135);l2.Size =newSize(126, 20);l2.Text ="Phone number:";l2.Font =newFont("Bauhaus 93", 12);// Adding label on the formthis.Controls.Add(l2);// Creating and setting the// properties of the MaskedTextBoxMaskedTextBox mbox =newMaskedTextBox();mbox.Location =newPoint(374, 137);mbox.Mask ="000000000";mbox.Size =newSize(176, 20);mbox.Name ="MyBox";mbox.Font =newFont("Bauhaus 93", 18);// Adding MaskedTextBox// control on the formthis.Controls.Add(mbox);}}}chevron_rightfilter_noneOutput:

Constructor
| Constructor | Description |
|---|---|
| MaskedTextBox() | This Constructors is used to initialize a new instance of the MaskedTextBox class. |
| MaskedTextBox(MaskedTextProvider) | This Constructors is used to initialize a new instance of the MaskedTextBox class using the specified custom mask language provider. |
| MaskedTextBox(String) | This Constructors is used to initialize a new instance of the MaskedTextBox class using the specified input mask. |
Properties
| Property | Description |
|---|---|
| AsciiOnly | Gets or sets a value indicating whether the MaskedTextBox control accepts characters outside of the ASCII character set. |
| AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
| BackColor | This property is used to get or set the background color for the control. |
| BorderStyle | This property indicates the border style for the control. |
| Font | This property is used to get or set the font of the text displayed by the control. |
| ForeColor | This property is used to get or set the foreground color of the control. |
| Height | This property is used to get or set the height of the control. |
| Location | This property is used to get or set the coordinates of the upper-left corner of the MaskedTextBox control relative to the upper-left corner of its form. |
| Name | This property is used to get or set the name of the control. |
| TabStop | This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the NumericUpDown. |
| Size | This property is used to get or set the height and width of the control. |
| Text | This property is used to get or set the text to be displayed in the RichTextBox control. |
| Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
| Width | This property is used to get or set the width of the control. |
| Multiline | This property is used to get or set a value indicating whether this is a multiline MaskedTextBox control. |
| TextAlign | This property is used to get or set how text is aligned in a masked text box control. |
| TextMaskFormat | This property is used to get or set a value that determines whether literals and prompt characters are included in the formatted string. |
| SelectedText | This property is used to get or set the current selection in the MaskedTextBox control. |
| PromptChar | This property is used to get or set the character used to represent the absence of user input in MaskedTextBox. |
| ReadOnly | This property is used to get or set a value indicating whether text in the text box is read-only. |
| MaxLength | This property is used to get or set the maximum number of characters the user can type or paste into the text box control. This property is not supported by MaskedTextBox. |
| Lines | This property is used to get or set the lines of text in multiline configurations. This property is not supported by MaskedTextBox. |
Recommended Posts:
- How to set the Name of the MaskedTextBox in C#?
- How to set the Visibility of MaskedTextBox in C#?
- How to set the Font of the MaskedTextBox in C#?
- How to set the Location of the MaskedTextBox in C#?
- How to set the size of the MaskedTextBox in C#?
- How to set Text in MaskedTextBox in C#?
- How to set the Prompt Character in MaskedTextBox in C#?
- How to set the Password Character for MaskedTextBox in C#?
- How to set ASCII Characters in MaskedTextBox in C#?
- How to style the Border of the MaskedTextBox in C#?
- How to Mask input in MaskedTextBox in C#?
- How to set the Foreground Color of the MaskedTextBox in C#?
- How to set the Background Color of the MaskedTextBox in C#?
- How to set the Alignment of the Text in MaskedTextBox in C#?
- How to set Read-Only content in MaskedTextBox in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



