MaskedTextBox Control in C#

In this article, we will learn how to use MaskedTextBox in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a MaskedTextBox then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose " MaskedTextBox".

  1. First of all, we'll know about the "MaskedTextBox".

    MaskedTextBox uses a mask to distinguish between proper and improper user input. We'll discuss properties and functions and events of MaskedTextBox write down.

    "BackColor" changes the background color of MaskedTextBox control.

    maskedTextBox1.BackColor = Color.AliceBlue;

    "BorderStyle" changes the style of maskedTextBox control border.

    maskedTextBox1.BorderStyle = BorderStyle.FixedSingle;

    "Click" when user mouse click on maskedTextBox control.

    maskedTextBox1.Click+=new EventHandler(maskedTextBox1_Click);
    private void maskedTextBox1_Click(object sender, EventArgs e)
        MessageBox.Show("You click on maskedtextbox");

    "Cursor" when user mouse hover on maskedTextBox control then cursor style changed.

    maskedTextBox1.Cursor = Cursors.Hand;

    "Dispose()" released all resources used by the System.ComponentModel.Component.


    "DoubleClick" when user mouse double click on maskedTextBox control.

    maskedTextBox1.DoubleClick+=new EventHandler(maskedTextBox1_DoubleClick);
    private void maskedTextBox1_DoubleClick(object sender, EventArgs e)
        MessageBox.Show("You double click on maskedtextbox");

    "Enabled" changes the maskedTextBox control are enable or not.

    maskedTextBox1.Enabled = true;

    "Focus()" focusing the maskedTextBox control.


    "Font" changes the style of the text of maskedTextBox control.

    maskedTextBox1.Font = new System.Drawing.Font("arial", 14.0F, FontStyle.Bold);

    "ForeColor" changes the font color of maskedTextBox control.

    maskedTextBox1.ForeColor = Color.AliceBlue;

    "Height" set the height of maskedTextBox control.


    "Hide()" hides the maskedTextBox control.


    "MouseClick" when user click the maskedTextBox by mouse.

    maskedTextBox1.MouseClick+=new MouseEventHandler(maskedTextBox1_MouseClick);
    private void maskedTextBox1_MouseClick(object sender, MouseEventArgs e)
        MessageBox.Show("You mouse click on maskedtextbox");

    "MouseDoubleClick" when user double click the by mouse.

    maskedTextBox1.MouseDoubleClick += new MouseEventHandler(maskedTextBox1_MouseDoubleClick);
    private void maskedTextBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        MessageBox.Show("You mouse Double click on maskedtextbox");

    "MouseEnter" when mouse pointer enters the maskedTextBox control.

    maskedTextBox1.MouseEnter+=new EventHandler(maskedTextBox1_MouseEnter);
    private void maskedTextBox1_MouseEnter(object sender, EventArgs e)
        MessageBox.Show("you mouse enter on maskedtextbox");

    "MouseHover" when mouse pointer rests/hover on the maskedTextBox control.

    maskedTextBox1.MouseHover += new EventHandler(maskedTextBox1_MouseHover);
    private void maskedTextBox1_MouseHover(object sender, EventArgs e)
        MessageBox.Show("you mouse Hover on maskedtextbox");

    "MouseLeave" when mouse pointer leaves the maskedTextBox control.

    maskedTextBox1.MouseLeave+=new EventHandler(maskedTextBox1_MouseLeave);
    private void maskedTextBox1_MouseLeave(object sender, EventArgs e)
        MessageBox.Show("you mouse leave on maskedtextbox");

    "MouseMove" when mouse pointer moves the maskedTextBox control.

    maskedTextBox1.MouseMove+=new MouseEventHandler(maskedTextBox1_MouseMove);
    private void maskedTextBox1_MouseMove(object sender, MouseEventArgs e)
        MessageBox.Show("you mouse move on masktextbox");

    "Mask" sets the string governing the input allowed for this control.


    "Paste()" Replaces the current selection in the textbox with the contents of the clipboard.


    "PasswordChar" gets or sets the character to be displayed in subtitute for user input.

    maskedTextBox1.PasswordChar = '#';

    "ReadOnly" gets or sets a value indicating whether text in the text box is read-only.

    maskedTextBox1.ReadOnly = true;

    "ResetText()" resets all properties of maskedTextBox to its default values.


    "Show()" displays the maskedTextBox control.


    "Text" gets or sets text on maskedTextBox control.

    maskedTextBox1.Text = "Submit";

    "TextAlign" gets or sets text align on maskedTextBox control.

    maskedTextBox1.TextAlign = ContentAlignment.MiddleRight;

    "TextAlign" gets or sets text align on maskedTextBox control.

    maskedTextBox1.TextAlign = HorizontalAlignment.Center;

    "UseSystemPasswordChar" gets or sets value indicating whether the operating system-supplied password character should be used.

    maskedTextBox1.UseSystemPasswordChar = true;

    "Visible" gets or sets the maskedTextBox control visible or not.

    maskedTextBox1.Visible = true;

Write a comment

Name Email Message