Windows Forms 2.0 MaskedTextBox Control


Visual Studio 2005 now provides a new control - MaskedTextBox.

MaskedTextBox control is a TextBox with mask feature, which allows you to set the format of the data of TextBox. If you drag a MaskedTextBox to the form, you will see an option to set the mask. Figure 1.

MaskedTB1.jpg

Figure 1.  MaskedTextBox control.

The Input Mask dialog allows you to set various masks such as Zip code, Time, and phone number. See Figure 2.

MaskedTB2.jpg

Figure 2. Input Mask.

Try it option at the bottom allows you to test the mask. See Figure 3.

MaskedTB3.jpg

Figure 3. Input Mask Try it.

Now I create  a form with social security, zip code, and phone number masks. See Figure 4.

MaskedTB4.jpg

Figure 4. Setting up various masks.

When I run the application, output looks like Figure 5.

MaskedTB5.jpg

Figure 5. Various masks in action.

The Mask property of MaskedTextBox allows you to set the Mask programmatically. The following code sets masks programmatically what we have set in the above sample using Input Mask dialog.

this.maskedTextBox1.Mask = "00000-9999";
this.maskedTextBox2.Mask = "000-00-0000";
this.maskedTextBox3.Mask = "(999)000-0000";