How to set the Text Mask Format of the MaskedTextBox in C#?
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. In MaskedTextBox control, you set a value which indicates that the literals and the prompt characters are included in the formatted string present in the MaskedTextBox using TextMaskFormat Property provided by the MaskedTextBox control. This property has four values and these values are defined under MaskFormat enum and the values are:
- The ExcludePromptAndLiterals value returns the text input, which is provided by the user.
- The IncludeLiterals value returns the text input, which is provided by the user and the literal characters that are defined in the mask.
- The IncludePrompt value returns the text input, which is provided by the user and the instances of the prompt character.
- The IncludePromptAndLiterals value provides the text input, which is given by the user, the literal characters that are defined in the mask and the instances of the prompt character.
The default value of this property is IncludeLiterals. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the value of TextMaskFormat property of MaskedTextBox control 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 on the form. As shown in the below image:

- Step 3: After drag and drop you will go to the properties of the MaskedTextBox and set the value of TextMaskFormat property of MaskedTextBox control as shown in the below image:

Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the value of TextMaskFormat property of the MaskedTextBox control programmatically with the help of given syntax:
public System.Windows.Forms.MaskFormat TextMaskFormat { get; set; }
Here, MaskFormat holds the values of the TextMaskFormat property. If the value of this property does not belong to the MaskFormat enum, then it will throw an InvalidEnumArgumentException. The following steps show how to set the value of the TextMaskFormat property of the MaskedTextBox control dynamically:
- Step 1: Create a MaskedTextBox using the MaskedTextBox() constructor is provided by the MaskedTextBox class.
// Creating a MaskedTextBox MaskedTextBox m = new MaskedTextBox();
- Step 2: After creating MaskedTextBox, set the TextMaskFormat property of the MaskedTextBox provided by the MaskedTextBox class.
// Setting the TextMaskFormat property m.TextMaskFormat = MaskFormat.IncludeLiterals;
- Step 3: And last add this MaskedTextBox control to the form using the following statement:
// Adding MaskedTextBox control on the form this.Controls.Add(m);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp39 {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("Bell MT", 12);// Adding label on the formthis.Controls.Add(l1);// Creating and setting the// properties of LabelLabel l2 =newLabel();l2.Location =newPoint(242, 135);l2.Size =newSize(126, 20);l2.Text ="Phone number:";l2.Font =newFont("Bell MT", 12);// Adding label on the formthis.Controls.Add(l2);// Creating and setting the// properties of MaskedTextBoxMaskedTextBox m =newMaskedTextBox();m.Location =newPoint(374, 137);m.Mask ="000000000";m.Size =newSize(176, 20);m.Name ="MyBox";m.BorderStyle = BorderStyle.Fixed3D;m.TextMaskFormat = MaskFormat.IncludeLiterals;m.Font =newFont("Bell MT", 18);// Adding MaskedTextBox// control on the formthis.Controls.Add(m);}}}chevron_rightfilter_noneOutput:

Recommended Posts:
- How to Mask input in MaskedTextBox in C#?
- How to set Text in MaskedTextBox in C#?
- How to set the Alignment of the Text in MaskedTextBox in C#?
- How to Highlight the Text Present in the MaskedTextBox in C#?
- How to set the Name of the MaskedTextBox in C#?
- C# | MaskedTextBox Class
- How to set the Visibility of MaskedTextBox in C#?
- How to set the Location of the MaskedTextBox in C#?
- How to set the Font of the MaskedTextBox in C#?
- How to set the size of the MaskedTextBox in C#?
- How to set the Background Color of the MaskedTextBox in C#?
- How to set Read-Only content 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#?
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.



