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.
public partial class Form1 : Form
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
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 then entered number will show in the Label.
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.
private void frmMaskedTextBox_Load(object sender, EventArgs e)
//change backcolor of maskedTextBox
maskedTextBox1.BackColor = Color.CadetBlue;
Next Article :-
Date Validation in C Sharp .NET