How to: Create 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 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.

This example checks 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

  • Add the NumericTextBox class to your project.

    Public Class NumericTextBox
        Inherits TextBox
        Private SpaceOK As Boolean = False
        ' Restricts the entry of characters to digits (including hex),
        ' the negative sign, the e decimal point, and editing keystrokes (backspace).
        Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
            Dim numberFormatInfo As NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
            Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
            Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
            Dim negativeSign As String = numberFormatInfo.NegativeSign
            Dim keyInput As String = e.KeyChar.ToString()
            If [Char].IsDigit(e.KeyChar) Then
                ' Digits are OK
            ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
                ' Decimal separator is OK
            ElseIf e.KeyChar = vbBack Then
                ' Backspace key is OK
                '    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
                '    {
                '     // Let the edit control handle control and alt key combinations
                '    }
            ElseIf Me.SpaceOK AndAlso e.KeyChar = " "c Then
                ' Consume this invalid key and beep.
                e.Handled = True
            End If
        End Sub
        Public ReadOnly Property IntValue() As Integer
                Return Int32.Parse(Me.Text)
            End Get
        End Property
        Public ReadOnly Property DecimalValue() As Decimal
                Return [Decimal].Parse(Me.Text)
            End Get
        End Property
        Public Property AllowSpace() As Boolean
                Return Me.SpaceOK
            End Get
            Set(ByVal value As Boolean)
                Me.SpaceOK = value
            End Set
        End Property
    End Class

To add the NumericTextBox control to the form

  1. 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)
  2. Add an InputPanel component to your form for user input into the NumericTextBox. For a Smartphone application, you can specify a numeric InputMode.