articles

Home / DeveloperSection / Articles / MaskedTextBox Control in C#.Net

MaskedTextBox Control in C#.Net

MaskedTextBox Control in C#.Net

Pushpendra Singh 25645 24-Jan-2011

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.

MaskedTextBox Control in C#.Net

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

 

MaskedTextBox Control in C#.Net

 

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 Control in C#.Net

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

MaskedTextBox Control in C#.Net

Next Article :- Date Validation in C Sharp .NET
https://www.mindstick.com/articles/55/date-validation-in-c-sharp-dot-net


c#c# 
Updated 04-Mar-2020

Leave Comment

Comments

Liked By