Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
How to create MaskedTextBox?
Creating MaskedTextBox in Run-Time
MaskedTextBox Properties
Frequently Asked Questions
Is C# different from C++?
What is C# used for?
Why is C# so popular?
Which is faster C++ or C#?
Is C# or C++ better for games?
Last Updated: Mar 27, 2024

C# MaskedBox Class

Author Shiva
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


A MaskedTextBox control on a Form acts as a validation technique for user input. You may use masking in the MaskedTextBox to make a TextBox accept a date in mm/dd/yyyy format, for example.

In this article, we'll discuss how to construct a MaskedTextBox control in Windows Forms at both design and runtime. Following that, we'll go over the many properties and methods available for the MaskedTextBox control.

MaskedBox class is used to create a mask that distinguishes between correct and incorrect user input. The MaskedTextBox class is used to represent the masked text box in Windows and provides several attributes, methods, and events. 

The namespace System.Windows.Forms contain their definition.

How to create MaskedTextBox?

At design time, we can use a Forms designer to create a MaskedTextBox control, or at run time, we can use the MaskedTextBox class in code (also known as dynamically).

Simply drag and drop a MaskedTextBox control from the Toolbox to a Form in Visual Studio to create a MaskedTextBox control at design time. The figure below shows what a MaskedTextBox looks like after being dragged and dropped onto a Form. You can use the mouse to move and resize a MaskedTextBox on the Form, as well as set its settings and events.


To create this step by step process is given below: 
Step 1: Create a new project and locate WindowFormApp

Step 2: Locate MaskedTextBox control from the toolbox and simply drag and drop to the form.

Step 3: Customize the MaskedTextBox accordingly.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Creating MaskedTextBox in Run-Time

To make a dynamic MaskedTextBox, first, make an instance of the MaskedTextBox class. A MaskedTextBox control object is created with the code below.

MaskedTextBox dynamicMaskedTextBox = newMaskedTextBox();


The characteristics of a MaskedTextBox control can be set in the next step. The following code snippet changes the MaskedTextBox's background color, foreground color, Text, Name, and Font attributes.

dynamicMaskedTextBox.BackColor = Color.Red; 
dynamicMaskedTextBox.ForeColor = Color.Blue; 
dynamicMaskedTextBox.Text = "This is a Dynamic MaskedTextBox"; dynamicMaskedTextBox.Name = "DynamicMaskedTextBox"; 
dynamicMaskedTextBox.Font = newFont("Consolas", 18);

The next step is to add the MaskedTextBox control to the Form once it has been configured with its properties. The method (Form. Controls. Add) is used to accomplish this. The MaskedTextBox control is added to the current Form with the following code snippet.



MaskedTextBox Properties

Setting properties after placing a MaskedTextBox control on a Form is the next step.

The Properties Window is the most convenient way to change properties. You can enter the Properties window by pressing F4 or selecting Properties from the context menu.

Here is the list of common properties that are used in MaskedTestBox:


dynamicMaskedTextBox.Location = newPoint(20, 150); 


dynamicMaskedTextBox.Height = 40; 


dynamicMaskedTextBox.Width = 300; 


dynamicMaskedTextBox.BackColor = Color.Red; 


dynamicMaskedTextBox.ForeColor = Color.Blue; 


dynamicMaskedTextBox.BorderStyle = BorderStyle.FixedSingle;


Also see,  Ienumerable vs Iqueryable

Frequently Asked Questions

Is C# different from C++?

C++ is an intermediate-level language that extends C with object-oriented capabilities, whereas C# is a high-level language. C++ compiles programs to Machine Codes, whereas C# compiles programs to CLR or Common Language Runtime.

What is C# used for?

C# may be used to construct a variety of programs and applications, including mobile apps, desktop apps, cloud-based services, websites, enterprise software, and games, just like other general-purpose programming languages.

Why is C# so popular?

C# has the advantage of being able to construct applications for both mobile and computer devices. It is also extensively used by the open-source community to create software applications utilizing various frameworks like Mono and the.NET Framework.

Which is faster C++ or C#?

C++ code is substantially faster than C# code, making it a superior choice for applications that require high speed. Your network analysis software, for example, may require C++ code, but performance is unlikely to be an issue for a conventional word processing application written in C#.

Is C# or C++ better for games?

C# and C++ can both be used to make games. C++, on the other hand, has better hardware control on the PC or server. As a result, it's usually a better choice for game creation. Both languages, on the other hand, are for game creation, especially since you won't be making games from scratch (usually).


In this article, we have extensively discussed MaskedBox class in C#. We hope that this blog has helped you enhance your knowledge regarding the MaskedBox Class and if you would like to learn more, check out our articles on Basics of C#Methods in C#, and Type Conversion in C#.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Live masterclass