Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend


Getting users to input data in precisely the right form has always been a problem. For example, the simple act of entering a telephone number can result in a number of different forms. Some users might input the telephone number as 1-800-555-1212, while others input it as (800)555-1212. In fact, you can expect to see any number of inputs, all of which are correct, making searching for data in a database difficult (to say the least). The MaskedTextBox control fixes this problem by allowing data entry in only one form.

You can also see the MaskedTextBox as a means of reducing security risks on your system. A cracker is going to have a very hard time trying to inject a script when you only allow specific input. So, besides cuing the user on what input you want and making all of the input consistent, the MaskedTextBox can be a security aid as well.

In most ways, a MaskedTextBox works just as a regular TextBox control does. The difference is the Mask property. Click the ellipsis (...) in this property, and you get the Input Mask dialog box shown in Figure 3. Microsoft provides a number of familiar input masks, such as those for telephone numbers.

Figure 3

Figure 3 Use this dialog box to define the input requirements for the MaskedTextBox.

As shown in Figure 3, you provide a mask and the control provides a preview. In this case, the user must provide a value for every 0, but the 9 entries are optional. The hyphens provide formatting for the input. You can test the mask by typing values in the preview; incorrect values display an error. The MSDN web site provides a complete list of the special symbols used with this control.

  • + Share This
  • 🔖 Save To Your Account