代码之家  ›  专栏  ›  技术社区  ›  Tom

如何将文本框输入仅过滤为数字?

  •  2
  • Tom  · 技术社区  · 16 年前

    如何抑制除数字之外的所有数据?

    KeyDown() :

    If e.KeyData < Keys.D0 Or e.KeyData > Keys.D9 Then
        e.Handled = True
    End If
    
    9 回复  |  直到 13 年前
        1
  •  5
  •   Anonymous Pi Josh    11 年前

    有很多方法可以做到这一点。我快速尝试了一下,然后选择了这个有效的方法。我使用了文本框的KeyPress子项,并将每个按键传递给IsNumber函数。

    注意:我允许使用退格键,以防您在数字上出错并想要删除。

    拿出 如果e.KeyChar<>ChrW(按键返回)然后/结束如果 如果你不需要退格,就退一步。

        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then
            Else
                e.Handled = True
            End If
        End If
    End Sub
    
        2
  •  6
  •   kevchadders    16 年前

    你可以检查Char。IsDigit(e.KeyChar),但在这种情况下最好的办法是创建TextBox的子类并重写IsInputChar()。这样你就有了一个可重用的TextBox控件,你可以把它放在任何地方,这样你就不必重新实现逻辑。

    (我的VB有点生疏…)

    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
    
        3
  •  1
  •   Laxmikant Bhumkar    13 年前

    文本框 仅接受 数值 语句结束 当您不想接受退格键时,代码中的值。增强版

    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then
            Else
                e.Handled = True
            End If
        End If
    End Sub
    
        4
  •  1
  •   Duke49ifrance    12 年前

    会帮助你的。..

        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
    

    e.已处理=IsNumericTextbox(发送者,e.KeyChar)

        5
  •  1
  •   YaPKnn    11 年前
     Public Class NumericTextBox : Inherits System.Windows.Forms.TextBox
    
     Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
          If Char.IsDigit(e.KeyChar) Or
              Char.IsControl(e.KeyChar) Or
              e.KeyChar = lobalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator Then
          MyBase.OnKeyPress(e)
        Else
            e.Handled = True
        End If
     End Sub
    
     End Class
    
        6
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    13 年前

    我建议你使用正则表达式。你可以在谷歌上搜索,比如“仅限正则表达式文本框的数字”,我想你会想出很多例子。

    例如,如果你在ASP。NET,你可以这样做:

    <asp:TextBox 
        ID="txtPhoneNumber" 
        runat="server" 
        Text='<%#Bind("phoneNumber") %>' 
        MaxLength="15">
    </asp:TextBox>
    
    <asp:RegularExpressionValidator 
        ID="rfvUSerPhoneNumberValidate" 
        runat="server"
        ControlToValidate="txtPhoneNumber" 
        Display="Dynamic" 
        ValidationExpression="^[0-9]{1,15}$"
        ErrorMessage="Please enter only numeric value for Phone Number" 
        EnableViewState="true">
    </asp:RegularExpressionValidator>
    
        7
  •  0
  •   user2219334    12 年前

    这将允许数字输入,Backspace用于更正您的输入,以及小数点。

        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
    
        8
  •  0
  •   Ejie Fernandez    12 年前

    这是限制文本框中数字输入的另一种方法。使用KEYPRESS事件

    如果Asc(e.KeyChar)<>13 And Also Asc(e.KeyChar)<>8也不是数字(e.KeyChar)然后 MessageBox。显示(“仅数字”) e.已处理=真 语句结束 结束子

        9
  •  0
  •   Jon Milliken    9 年前

    您的功能的目的可以帮助提供其他解决方案。检查每个按键上的数值可能有点过头了。然后,你必须通过考虑退格、删除、复制、粘贴等来夸大它。