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
Declare a global variable in the form, as shown in the following code.
Add the following code to the form's constructor or Load event.
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.
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
Add two check box controls to the form.
Set the Text property of the first check box to allow decimal.
Set the Text property of the second check box to allow separators.
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