A MaskedTextBox control provides a validation mechanism for user input on a Form. For example, if you want a TextBox to accept a date in mm/dd/yyyy format, you can set masking in the MaskedTextBox.
In this article, I will discuss how to create a MaskedTextBox control in Windows Forms at design-time as well as run-time. After that, I will continue discussing various properties and methods available for the MaskedTextBox control.
Creating a MaskedTextBox
We can create a MaskedTextBox control using a Forms designer at design-time or using the MaskedTextBox class in code at run-time (also known as dynamically).
To create a MaskedTextBox control at design-time, you simply drag and drop a MaskedTextBox control from Toolbox to a Form in Visual Studio. After you drag and drop a MaskedTextBox on a Form, the MaskedTextBox looks like Figure 1. Once a MaskedTextBox is on the Form, you can move it around and resize it using mouse and set its properties and events.
In this article, I will discuss how to create a MaskedTextBox control in Windows Forms at design-time as well as run-time. After that, I will continue discussing various properties and methods available for the MaskedTextBox control.
Creating a MaskedTextBox
We can create a MaskedTextBox control using a Forms designer at design-time or using the MaskedTextBox class in code at run-time (also known as dynamically).
To create a MaskedTextBox control at design-time, you simply drag and drop a MaskedTextBox control from Toolbox to a Form in Visual Studio. After you drag and drop a MaskedTextBox on a Form, the MaskedTextBox looks like Figure 1. Once a MaskedTextBox is on the Form, you can move it around and resize it using mouse and set its properties and events.

Figure 1
Creating a MaskedTextBox control at run-time is merely a work of creating an instance of MaskedTextBox class, setting its properties and adding MaskedTextBox class to the Form controls.
The first step to create a dynamic MaskedTextBox is to create an instance of MaskedTextBox class. The following code snippet creates a MaskedTextBox control object.
- MaskedTextBox dynamicMaskedTextBox = newMaskedTextBox();
In the next step, you may set properties of a MaskedTextBox control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a MaskedTextBox.
- dynamicMaskedTextBox.BackColor = Color.Red;
- dynamicMaskedTextBox.ForeColor = Color.Blue;
- dynamicMaskedTextBox.Text = "I am Dynamic MaskedTextBox";
- dynamicMaskedTextBox.Name = "DynamicMaskedTextBox";
- dynamicMaskedTextBox.Font = newFont("Georgia", 16);
Once a MaskedTextBox control is ready with its properties, the next step is to add the MaskedTextBox control to the Form. To do so, we use Form.Controls.Add method. The following code snippet adds a MaskedTextBox control to the current Form.
- Controls.Add(dynamicMaskedTextBox);
Setting MaskedTextBox Properties
After you place a MaskedTextBox control on a Form, the next step is to set properties.
The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2.
After you place a MaskedTextBox control on a Form, the next step is to set properties.
The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2.

Figure 2
Location, Height, Width, and Size
The Location property takes a Point that specifies the starting position of the MaskedTextBox on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a MaskedTextBox control.
- dynamicMaskedTextBox.Location = newPoint(20, 150);
- dynamicMaskedTextBox.Height = 40;
- dynamicMaskedTextBox.Width = 300;
Background, Foreground, BorderStyle
BackColor and ForeColor properties are used to set background and foreground color of a MaskedTextBox respectively. If you click on these properties in Properties window, the Color Dialog pops up.
Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties.
BackColor and ForeColor properties are used to set background and foreground color of a MaskedTextBox respectively. If you click on these properties in Properties window, the Color Dialog pops up.
Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties.
- dynamicMaskedTextBox.BackColor = Color.Red;
- dynamicMaskedTextBox.ForeColor = Color.Blue;



Eduardo FernandezPosted Aug 17, 2020, 7:44 PM
How do you control is the date value is correct?. I downloaded the zip file, run it, and I can enter any numeric values into the date field, and the program did not detect it. Examples: 45/52/2020. Thanks...
Rade MikinPosted Nov 30, 2018, 1:10 AM
How to limit input to C # 6.0 in textBox.text only positive and negative integers and decimal numbers and not allow the input of letters or special characters, as in the case 0.12 / -0.121,12 / -1.12 123.45 / -123.45 123,456.78 / -123,456.78 1,234,567,890.12 / - 1,234,567,890.12 1 / -1 123 / -123 123,456,789.12 / -123,456,789.12
Rade MikinPosted Nov 30, 2018, 1:09 AM
How to limit input to C # 6.0 in textBox.text only positive and negative integers and decimal numbers and not allow the input of letters or special characters, as in the case
Lijo JosePosted Sep 19, 2014, 3:04 AM
how to set focus to a masked texbox... In my case txtMasked.focus() doesn't works..
Jair SouzaPosted Jun 26, 2014, 10:33 AM
Como ocultar a mascara em tempo de execu??o ?
tri lai minhPosted Apr 27, 2013, 8:56 AM
thank you!
jackson kumarPosted Apr 6, 2013, 2:16 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
jackson kumarPosted Apr 6, 2013, 2:15 AM
Thanks Sir
Ravi KumarPosted Feb 27, 2012, 6:28 AM
How we can set the value of 1 to 64 interger value in the maskedtextbox? Means user can only enter the 1 to 64 interger value in the textbox.
Tania EmmerechtsPosted Dec 6, 2010, 5:33 AM
Hi everybody, I'm using a masked textbox for my Date like this : 01/12/2010 Now the problem is, when the user uses a date with a leading 0, the masked textbox gives this 11/22/010_ So it skipped the leading 0 althought it is correct in my database. This is the code I use to fill the masked textbox : mtbShipDate.Text = Convert.ToString(dataRowData["ShipDate"]); Please can somebody help me? Thanks, Tania
Mayur GujrathiPosted Nov 1, 2010, 4:04 AM
I want to set date format as dd/mm/yyyy how can we do it. when i write maskedtextbox1.text=datetime.now.tostring it is getting format as mm/dd/yyyy