Partager via


How to: Control User Input in a Numeric Text Box

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can create a custom control that is derived from TextBox that accepts only numeric input. This topic shows how to define a NumericTextBox class and how to put the custom control on a form.

This custom control resembles the one described in How to: Create a Numeric Text Box. This implementation of the control uses a more restrictive method of controlling user input. This control accepts only a limited set of values, including digits, decimals, separators, and the negative sign. When the control accepts input from the keypad, it checks the whole input string and then re-displays only the valid characters.

To derive a class from TextBox

  • Add the NumericTextBox class to your project by using the following code.

To add the NumericTextBox control to the form

  1. Declare a global variable in the form, as shown in the following code.

  2. Add the following code to the form's constructor or Load event.

  3. For a Smartphone application, specify an InputMode by using the following code.

    Note

    For a Pocket PC application, add an InputPanel component to the form for user input into the numeric text box. For more information, see How to: Use the InputPanel Component.

  4. Use the following code to set the focus on the numeric text box.

To add controls that enable you to set properties of the numeric text box

  1. Add two check box controls to the form.

  2. Set the Text property of the first check box to allow decimal.

  3. Set the Text property of the second check box to allow separators.

  4. Add the following event handlers for the check boxes to the form.

Compiling the Code

This example requires references to the following namespaces:

See Also

Tasks

How to: Set Smartphone Input Modes

Concepts

Custom Control Development

.NET Compact Framework How-to Topics