MaskedTextBox Control in C#.Net

.Net Developer
2 min readJan 1, 2020

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 and invalid user input. Now our next question is -

How to use MaskedTextBox Control ?

First of All the Drag and drop MaskedTextBox control and a button from toolbox on the window Form.

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;
}
}
}

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

When you enter number andclick submit button then entered number will show in the Label.

MaskedTextBox Properties

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

Specifies the input mask to use at run time.

Set BackColor of MaskedTextBox.

Example:

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

}

Output

Next Article :- Date Validation in C Sharp .NET

--

--

.Net Developer

I’m a professional Technical writer and Software Development with more than 5 years of experience.