共用方式為


MaskedTextBox Control Sample

Illustrates the key features of this control.

The MaskedTextBox Control Sample demonstrates the use of several regular and advanced features of the control. These features include defining custom masks, validating user input, and adding new mask definitions to Visual Studio's Input Mask editor.

For information about using the samples, see the following topics:

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To build and run the sample using the command prompt

  1. At the command prompt, navigate to the location where you saved the uncompressed sample files.

  2. Navigate to one of the language-specific subdirectories under the MaskedTextBoxSample directory.

  3. Type msbuild to build the sample using the solution (.sln) file provided.

  4. Type cd MaskedTextBox\bin\Debug to navigate to the location of the executable file.

  5. Type MaskedTextBoxSample.exe to run the sample.

To build and run the sample using Visual Studio

  1. In Windows Explorer, navigate to the location where you saved the uncompressed sample files.

  2. Navigate to one of the language-specific subdirectories under the MaskedTextBoxSample directory.

  3. Double-click the solution (.sln) file to open it in Visual Studio.

  4. In Visual Studio, press F5 to build and run the sample.

To use the MaskedTextBoxSample

  1. Enter various types of IP addresses into the MaskedTextBox and watch what happens—particularly when you enter addresses that are not valid, such as 300.300.300.300.

  2. Use the various check box options to control the output from the MaskedTextBox control's Text property.

Demonstrates

  • Controlling the text output of the control using the TextMaskFormat and CutCopyMaskFormat properties.

  • Controlling how and when the mask prompt is displayed using the PromptChar and HidePromptOnLeave properties

  • Defining a custom mask for IPv5 addresses, and including this custom mask in Visual Studio's Input Mask editor using the MaskDescriptor class.

  • Defining a custom data type for IPv5 addresses that can be supplied as an argument to the control's ValidatingType property.

  • Handling the TypeValidationCompleted event when the text in the control does not validate against ValidatingType.

  • Converting the string in the MaskedTextBox control into an instance of the IPv5 class.

See Also

Tasks

Walkthrough: Working with the MaskedTextBox Control
How to: Bind Data to the MaskedTextBox Control

Reference

MaskedTextBox

Other Resources

Windows Forms Controls Samples