Visual Studio 2005 includes a masked
editing control in its Toolbox—courtesy of .NET 2.0. It allows you to
restrict the input from a user and control how it is displayed by using
a mask. For example, you might want
to use a telephone mask so that when a user enters
6175551212, the mask will render the input
A mask can block invalid characters (such as %) and can signal to the user what is expected (for example, the parentheses indicate that an area code is required).
Create a new Windows Forms application project, name it
MaskedEntry, and drag a
MaskedTextBox control to the form created by the Visual Studio
Designer. Click the smart tag and choose the one action available:
"Set the mask associated with the control." The Input Mask dialog
opens, as shown in Figure
The Framework provides you with preconfigured masks. .
Figure 3-9. Masked text box Input Mask dialog
As you can see, .NET 2.0 provides you with a number of standard masks. Choose the "Phone number" mask; the mask appears in the Mask text box, and you are invited to try it out in the "Try it" text box, as shown in Figure 3-10.
Figure 3-10. Trying the "Phone number" mask
If you are happy with the mask you've selected, click the OK button and the mask you selected ...