Professional Documents
Culture Documents
ARTICLE
MaskedTextBox in C#
By Mahesh Chand on Jul 16, 2010
In this article, I will discuss how to create a MaskedTextBox control in Windows Forms at
design-time as well as run-time.
225.9k
14
google Plus
o
o
o
o
o
o
o
o
o
o
o
o
o
expand
Download Files:
MaskedTextBoxSampleInCSharp.zip
Download Aspose, it really helps
A MaskedTextBox control provides validation mechanism for user input on a Form. For
example, if you want a TextBox to accept 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.
Figure 1
Creating a MaskedTextBox control at run-time is merely a work of creating an
instance of MaskedTextBox class, set its properties and add MaskedTextBox class to
the Form controls.
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 = new MaskedTextBox();
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 = new Font("Georgia", 16);
Once a MaskedTextBox control is ready with its properties, 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);
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
You can also set borders style of a MaskedTextBox by using the BorderStyle
property. The BorderStyle property is represented by a BorderStyle enumeration
that has three values FixedSingle, Fixed3D, and None. The default value of
border style is Fixed3D. The following code snippet sets the border style of a
MaskedTextBox to FixedSingle.
dynamicMaskedTextBox.BorderStyle = BorderStyle.FixedSingle;
Name
Name property represents a unique name of a MaskedTextBox control. It is used to
access the control in the code. The following code snippet sets and gets the name
and text of a MaskedTextBox control.
dynamicMaskedTextBox.Name = "DynamicMaskedTextBox";
string name = dynamicMaskedTextBox.Name;
Font
Font property represents the font of text of a MaskedTextBox control. If you click on
the Font property in Properties window, you will see Font name, size and other font
options. The following code snippet sets Font property at run-time.
Maximum Length
You can restrict number of characters in a MaskedTextBox control by setting MaxLength
property. The following code snippet sets the maximum length of a MaskedTextBox to 50
characters.
dynamicMaskedTextBox.ReadOnly = true;
dynamicMaskedTextBox.MaxLength = 50;
Figure 3
Set Mask launches Input Mask dialog where we can select a mask and you can see its data
format and also set the validation type. See Figure 4.
Figure 4
The following code snippet sets the Mask property at run-time.
dynamicMaskedTextBox.Mask = "00/00/0000";
MaskFull
MaskFull property represents if the mask values are completed.
if (maskedTextBox1.MaskFull){}
BeepOnError
If set true, the system beep is generated when incorrect values are entered.
dynamicMaskedTextBox.BeepOnError = true;
Summary
A MaskedTextBox control accepts user input on a Form. In this article, we discussed discuss
how to create a MaskedTextBox control in Windows Forms at design-time as well as run-time.
After that, we saw how to use various properties and methods.