How to filter textbox input to numeric only?

前端 未结 9 1460
耶瑟儿~
耶瑟儿~ 2020-12-21 13:37

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
          


        
相关标签:
9条回答
  • 2020-12-21 14:29

    Will help you...

        Public Function IsNumericTextbox(ByVal sender As TextBox, ByVal KeyChar As Char) As Boolean
        'set TRUE: cause a exception when the keychar is not Allowed into vars: allowedChars, allowedOneChar, allowedExceptionChar
        Dim UseThrowDebuggy As Boolean = False
    
        Dim allowedChars As String = "0123456789"
        Dim allowedOnceChar As Char() = {"."}
        Dim allowedExceptionChar As Keys() = {Keys.Back}
    
        Dim idxAllowedNotFound As Integer
        Dim idxCountOne As Integer = 0
    
        idxAllowedNotFound = allowedChars.IndexOf(KeyChar)
        If idxAllowedNotFound = True Then
            'AllowedOnce
            For Each _c As Char In allowedOnceChar
                If _c = KeyChar Then
                    'Count Check
                    For Each _cc As Char In sender.Text
                        If _c = _cc Then idxCountOne += 1
                    Next
                    If idxCountOne = 0 Then
                        Return False
                    Else
                        Return True
                    End If
                End If
            Next
            'Exceptions
            For i As Integer = 0 To allowedExceptionChar.Count - 1
                If Asc(KeyChar) = Convert.ToUInt32(allowedExceptionChar(i)) Then Return False
            Next
            'Not Throw
            If UseThrowDebuggy = False Then
                If Char.IsNumber(KeyChar) Then
                    Return False
                Else
                    Return True
                End If
            End If
            'Outside to end for throw
        Else
            'AllowedChars
            Return False
        End If
    
        Dim _kc As String = ControlChars.NewLine & "Char: " & KeyChar & ControlChars.NewLine & "Asc: " & Asc(KeyChar) & ControlChars.NewLine
        Throw New Exception("UseThrowDebuggy found a unknow KeyChar: " & _kc)
    End Function
    

    For use my function add this code into a textbox_keypress:

    e.Handled = IsNumericTextbox(sender, e.KeyChar)

    0 讨论(0)
  • 2020-12-21 14:29

    This will allow numeric input ,Backspace to correct your input , and also a decimal point.

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> ControlChars.Cr AndAlso e.KeyChar <> "." Then
            Beep()
            e.Handled = True
        End If
    
    0 讨论(0)
  • 2020-12-21 14:31

    The purpose of your function could help provide additional solutions. Checking for a numeric value on each KeyPress is likely overkill. Then you have to overkill it more by accounting for backspace, delete, copy, paste, etc.

    For example, if you are storing a telephone number, you should use the "IsNumeric" function on the validate and update step. Alternatively, if you are selecting quantity of an item "NumericUpDown" control would be more appropriate than a TextBox.

    0 讨论(0)
提交回复
热议问题