C# | MaskedTextBox Class - GeeksforGeeks

05 Sep, 2019
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:
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(); 

// 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); 

// 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:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp36 {  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:
// Adding MaskedTextBox 
// control on the form 
this.Controls.Add(mbox); 

Example:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp36 {  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:
CSharp-Windows-Forms-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to calculate Electricity Bill
Introduction to .NET Framework
HashSet in C# with Examples
Common Language Runtime (CLR) in C#
C# | Replace() Method
How to find the length of an Array in C#
C# | Dictionary.Add() Method
C# Tutorial
C# | Class and Object
C# | How to use strings in switch statement