O'Reilly logo

Visual C# 2005: A Developer's Notebook by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Allow Valid Input Only

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 as (617) 555-1212.

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).

How do I do that?

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 3-9.

Note

The Framework provides you with preconfigured masks. .

Masked text box Input Mask dialog

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.

Trying the "Phone number" mask

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required