Tom Tom - 1 year ago 150
Vb.net Question

How to filter textbox input to numeric only?

How do I suppress all data except numeric?

This is not working on

KeyDown()
:

If e.KeyData < Keys.D0 Or e.KeyData > Keys.D9 Then
e.Handled = True
End If

Answer Source

You can check Char.IsDigit(e.KeyChar), but the best thing to do in this case is to create a subclass of TextBox and override IsInputChar(). That way you have a reusable TextBox control that you can drop anywhere so you don't have to re-implement the logic.

(My VB is a bit rusty...)

Public Class NumericTextBox : Inherits TextBox

    Protected Overrides Function IsInputChar(Byval charCode As Char) As Boolean
        If (Char.IsControl(charCode) Or Char.IsDigit(charCode)) Then
            Return MyBase.IsInputChar(charCode)
        Else
            Return False
        End If
    End Function

End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download