Home > DeveloperSection > Beginner > MaskedTextBox Control in C#.Net

MaskedTextBox Control in C#.Net


C# C# 
Ratings:
0 Comment(s)
 14098  View(s)
Rate this:

MaskedTextBox Control in C#.Net

A MaskedTextBox control provides validation mechanism for user input on a Form. It uses a mask to distinguish between valid and invalid user input.

How to use MaskedTextBox Control

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 frmMaskedTextBox_Load(object sender, EventArgs e)

{

            //change backcolor of maskedTextBox

            maskedTextBox1.BackColor = Color.CadetBlue;

 

}

 

Output

MaskedTextBox Control in C#.Net


Don't want to miss updates? Please click the below button!

Follow MindStick