A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-maskedtextbox-class/ below:

C# - MaskedTextBox Class - GeeksforGeeks

C# - MaskedTextBox Class

Last Updated : 19 May, 2025

In C#, the MaskedTextBox class control provides a validation procedure for the user input on the form, like date, phone numbers, etc. It provides a mask to enforce valid input formats. The MaskedTextBox class in C# is used to represent the Windows masked text box and also provides different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.

What is MaskedTextBox?

This class-enhanced version of the TextBox control supports a declarative syntax for receiving or rejecting the user input, and when this control displays at run time, it represents the mask as a sequence of prompt characters and optional literal characters.

Use Cases of MaskedTextBox:

The use cases of MaskedTextBox are listed below:

Ways to Create a MaskedTextBox In Windows Forms

There are mainly two ways to create a MaskedTextBox in Windows forms, which are mentioned below.

1. Drag and Drop (Design-Time)

This is the easiest way to create a MaskedTextBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the button on the form in the designer, and further we can change the appearance of the MaskedTextBox using the properties.

Follow these steps to create a MaskedTextBox.

Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.

Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

Step 3. Now open the common controls and drag and drop the MaskedTextBox on the form where we want to it be placed.

Step 4: Now open the properties of the button Right-click on the MaskedTextBox to open the Properties window, where you can customize its appearance and behavior.

In the properties, we can add different types of input text such as date and time.

Output:

2. Custom MaskedTextBox (Run Time)

In this method, we are going to modify Form1.cs file and add custom code modification in C# with the help of the MaskedTextBox class.

The following steps show how to create a MaskedTextBox dynamically:

Step 1: Create a MaskedTextBox control using the MaskedTextBox() constructor provided by the MaskedTextBox class.

// Creating a MaskedTextBox control

MaskedTextBox mbox = new MaskedTextBox();

Step 2: After creating the 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 lastly add this MaskedTextBox control to the form using the following statement:

// Adding MaskedTextBox

// control on the form

this.Controls.Add(mbox);

Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the
            // properties of the Label
            Label l1 = new Label();
            l1.Location = new Point(413, 98);
            l1.Size = new Size(176, 20);
            l1.Text = " Example";
            l1.Font = new Font("Bauhaus 93", 12);

            // Adding label on the form
            this.Controls.Add(l1);

            // Creating and setting the
            // properties of the Label
            Label l2 = new Label();
            l2.Location = new Point(242, 135);
            l2.Size = new Size(126, 20);
            l2.Text = "Phone number:";
            l2.Font = new Font("Bauhaus 93", 12);

            // Adding label on the form
            this.Controls.Add(l2);

            // Creating and setting the
            // properties of the MaskedTextBox
            MaskedTextBox mbox = new 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);

            // Adding MaskedTextBox
            // control on the form
            this.Controls.Add(mbox);
        }

    }
}

Output:

Constructor

This class consists of three constructors, with the help of which we can create objects of this class in different ways. The following are the constructors available in this class:

Constructor Description MaskedTextBox() This constructor is used to initialize a new instance of the MaskedTextBox class. MaskedTextBox(MaskedTextProvider) This constructor used to initialize a new instance of the MaskedTextBox class using the specified custom mask language provider. MaskedTextBox(String) This constructor is used to initialize a new instance of the MaskedTextBox class using the specified input mask. Properties

The properties of this class is listed below:

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 colour of 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 colour 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 the 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.
Troubleshooting Tips and Best Practices

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4