Updated: May 2009
You can create a custom control derived from TextBox so that it accepts only numeric input. This example defines the NumericTextBox class and shows how to place it on the form.
The code in this example verifies the current character whenever a key is pressed. Another way to implement a numeric text box is to use pattern matching to verify the entire input string. For more information, see IsMatch.
To derive a class from TextBox
To add the NumericTextBox control to the form
Add the following code to the form's constructor or Load event.
' Create an instance of NumericTextBox.
Dim NumericTextBox1 As NumericTextBox = New NumericTextBox()
NumericTextBox1.Parent = Me
' Draw the bounds of the NumericTextBox.
NumericTextBox1.Bounds = New Rectangle(5, 5, 150, 100)
// Create an instance of NumericTextBox.
NumericTextBox numericTextBox1 = new NumericTextBox();
numericTextBox1.Parent = this;
//Draw the bounds of the NumericTextBox.
numericTextBox1.Bounds = new Rectangle(5, 5, 150, 100);
Add an InputPanel component to your form for user input into the NumericTextBox. For a Smartphone application, you can specify a numeric InputMode.
This example requires references to the following namespaces:
Tasks
Concepts
Date | History | Reason |
|---|
May 2009
| Added information about regular expressions. |
Customer feedback.
|