---
title: "MaskedTextBox Control in C#.Net"  
description: "There is a MaskedTextBox control that provides validation mechanisms for user input on a Form. These are used as a mask to distinguish between valid"  
author: "Pushpendra Singh"  
published: 2011-01-24  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/407/maskedtextbox-control-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# MaskedTextBox Control in C#.Net

There is a MaskedTextBox [control](https://yourviews.mindstick.com/view/83439/bipartisan-gun-control-bill-passed-in-us-after-decades) that provides validation mechanisms for [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input) on a Form. These are used as a mask to distinguish between valid and invalid user input. Now our next [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) is -

## How to use MaskedTextBox Control ?

First of All the [Drag and drop](https://www.mindstick.com/forum/454/how-to-drag-and-drop-multiple-image-from-one-canvas-to-onther-canvas-in-html5) MaskedTextBox control and a button from toolbox on the [window Form](https://www.mindstick.com/forum/527/how-to-pass-datagridview-s-cells-value-to-another-window-form).

![MaskedTextBox Control in C#.Net](https://www.mindstick.com/mindstickarticle/29af393c-5a6e-4f18-92af-da89b0f8eae4/images/cc021a5d-2da3-414e-a695-8e1b0e3244e5.png)

## Code:

```
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms; namespace WindowsFormsApplication1{     public partial class Form1 : Form    {         public Form1()         {            InitializeComponent();         }         private void Form1_Load(object sender, EventArgs e)         {            // mask is specify that you can enter only number            maskedTextBox1.Mask = "000000000000000";         }         private void button1_Click(object sender, EventArgs e)         {            label1.Text = "You entered " + maskedTextBox1.Text;         }    }}
```

**Run the [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful)**

![MaskedTextBox Control in C#.Net](https://www.mindstick.com/mindstickarticle/29af393c-5a6e-4f18-92af-da89b0f8eae4/images/dc50429e-ec42-4fc3-a1ba-49d4280bb298.png)

When you enter other than number then masketextbox will not accept.Because in code mask is specified as maskedTextBox1.Mask = "00000000000000"; which means it will accept only number.

When you enter number and click [submit button](https://www.mindstick.com/forum/130/its-possible-without-submit-button) then entered number will show in the Label.

![MaskedTextBox Control in C#.Net](https://www.mindstick.com/mindstickarticle/29af393c-5a6e-4f18-92af-da89b0f8eae4/images/c71d65bf-b351-4259-8a9b-a5f3719f57bb.png)

**MaskedTextBox [Properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule)**

BeepOnError: Indicates whether the masked text box control raises the system beep for each user key stroke that it rejects.

Mask: Specifies the input mask to use at run time.

BackColor: Set BackColor of MaskedTextBox.

## Example:

```
private void frmMaskedTextBox_Load(object sender,  EventArgs e){            //change backcolor of maskedTextBox            maskedTextBox1.BackColor = Color.CadetBlue;  }
```

## Output

![MaskedTextBox Control in C#.Net](https://www.mindstick.com/mindstickarticle/29af393c-5a6e-4f18-92af-da89b0f8eae4/images/602705ed-3b29-486c-a563-7967716dc00d.png)

Next [Article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) :- [**Date Validation in C Sharp .NET**](https://www.mindstick.com/articles/55/date-validation-in-c-sharp-dot-net)\
https://www.mindstick.com/articles/55/date-validation-in-c-sharp-dot-net

---

Original Source: https://www.mindstick.com/articles/407/maskedtextbox-control-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
