Table of contents
1.
Introduction
2.
How to create MaskedTextBox?
3.
Creating MaskedTextBox in Run-Time
4.
MaskedTextBox Properties
5.
Frequently Asked Questions
5.1.
Is C# different from C++?
5.2.
What is C# used for?
5.3.
Why is C# so popular?
5.4.
Which is faster C++ or C#?
5.5.
Is C# or C++ better for games?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# MaskedBox Class

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

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.
 

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.
 

Controls.Add(dynamicMaskedTextBox);

 

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:

Location: 

dynamicMaskedTextBox.Location = newPoint(20, 150); 

Height: 

dynamicMaskedTextBox.Height = 40; 

Width: 

dynamicMaskedTextBox.Width = 300; 

Background: 

dynamicMaskedTextBox.BackColor = Color.Red; 

Foreground: 

dynamicMaskedTextBox.ForeColor = Color.Blue; 

BorderStyle: 

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).

Conclusion

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