Page 1 of 1

MaskedTextBox Tutorial 101 - Common Uses

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 26 November 2010 - 08:44 AM

Let’s see some common uses you might want to give your MaskedTextBox.

  • Format it to a date.
  • Accepts only letters.
  • Accepts only numbers.
  • Automatically add hyphens to phone numbers.


Let’s go!

Open Visual Studio 2010, and drag a MaskedTextBox control to the Winforms designer:

Posted Image


Now let’s write some code! First, we’ll have our maskedTextBox accept a date like format of digits only.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        maskedTextBox1.Mask = "00/00/0000";
        maskedTextBox1.MaskInputRejected +=
                            new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
    }

    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
        if (maskedTextBox1.MaskFull)
        {
            toolTip1.ToolTipTitle = "This textbox is full!";
            toolTip1.Show("This textbox is full.", maskedTextBox1, 200, 0, 2000);
        }
        else if (e.Position == maskedTextBox1.Mask.Length)
        {
            toolTip1.ToolTipTitle = "End of the field!";
            toolTip1.Show("You can't add extra input at the end of this.", maskedTextBox1, 200, 0, 2000);
        }
        else
        {
            toolTip1.ToolTipTitle = "Input Rejected";
            toolTip1.Show("You can only add numbers here.", maskedTextBox1, 200, 0, 2000);
        }
    }
}


Great! The .Mask property of the control is what tells it what to allow and not allow. You can read more about the .Mask element here: http://msdn.microsof...xtbox.mask.aspx

Accepting any amount of only letters.

MaskedTextBoxes don’t accept regular expressions in their .Mask property. Here’s a solution I found:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        maskedTextBox1.Mask = LetterCount(50);
        maskedTextBox1.MaskInputRejected +=
                            new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
    }

    private string LetterCount(int p)
    {
        StringBuilder builder = new StringBuilder(p);
        for (int i = 0; i < p; i++)
        {
            builder.Append("?");
        }
        return builder.ToString();
    }

    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
        if (maskedTextBox1.MaskFull)
        {
            toolTip1.ToolTipTitle = "This textbox is full!";
            toolTip1.Show("This textbox is full.", maskedTextBox1, 200, 0, 2000);
        }
        else if (e.Position == maskedTextBox1.Mask.Length)
        {
            toolTip1.ToolTipTitle = "End of the field!";
            toolTip1.Show("You can't add extra input at the end of this.", maskedTextBox1, 200, 0, 2000);
        }
        else
        {
            toolTip1.ToolTipTitle = "Input Rejected";
            toolTip1.Show("You can only add numbers here.", maskedTextBox1, 200, 0, 2000);
        }
    }
}



Since the MaskedTextBox doesn’t accept regular expressions, we have to get creative. This helper method returns n amount of the ‘?’ character, which allows any letter to be written.


Accepting any amount of only numbers.

Same drill as above. Except this time, we’ll use the ‘0’ mask element. (Zero)

private void Form1_Load(object sender, EventArgs e)
{
    maskedTextBox1.Mask = NumberCount(50);
    maskedTextBox1.MaskInputRejected +=
                        new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
}

private string NumberCount(int p)
{
    StringBuilder builder = new StringBuilder(p);
    for (int i = 0; i < p; i++)
    {
        builder.Append("0");
    }
    return builder.ToString();
}


Automatically adding hyphens to phone numbers.

private void Form1_Load(object sender, EventArgs e)
{
    maskedTextBox1.Mask = "(999)-000-0000";
    maskedTextBox1.MaskInputRejected +=
                        new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
}


We’re using the ‘9’ mask element to let our user either write a number in, or enter a space. Then we use the ‘0’ mask element to require our user to type in a number.

That about wraps it up! I hope you learned how to use this very helpful control. :D

Is This A Good Question/Topic? 0
  • +

Page 1 of 1