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;
}
}
}
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.
MaskedTextBox Properties
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 frmMaskedTextBoLoad(object sender, EventArgs e)
{
//change backcolor of maskedTextBox
maskedTextBox1.BackColor = Color.CadetBlue;
}
Output
Next Article :-
Date Validation in C Sharp .NET
https://www.mindstick.com/articles/55/date-validation-in-c-sharp-dot-net
Leave Comment