只接受数字输入的文本框

7
我发现了一个代码,可以让我的文本框只接受数字。
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim allowedChars As String = "0123456789"
    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If
End Sub

但是...用户不能使用退格键删除数字。那我该怎么办?
8个回答

9
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles                 txtValue.KeyPress
        'Dim allowedChars As String = "0123456789"
        'If allowedChars.IndexOf(e.KeyChar) = -1 Then
        '    ' Invalid Character
        '    e.Handled = True
        'End If
        'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
        '    e.Handled = True
        'End If
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub                                

7

您还需要处理粘贴的文本(可能没有按键)。最好的方法是使用MaskedTextBox


我明白了!但现在我有一个问题:我不想要提示字符。我只想要一个空的区域。我无法设置为没有提示字符。我尝试将其设置为空格,但本质上,空格仍然是一个字符.. - Saturn
嘿!我找到了处理粘贴文本的方法:选择textbox1,然后进入属性,将ShortcutsEnabled设置为“False”,完成 :) [voldemort] (http://stackoverflow.com/users/555690/voldemort) - Mousa Alfhaily

3

voldemort

我编写了您的第一份代码,使用户也能够删除。

以下是代码:

Dim BACKSPACE As Boolean

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Back Then
        BACKSPACE = True
    Else
        BACKSPACE = False
    End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If BACKSPACE = False Then
        Dim allowedChars As String = "0123456789"
        If allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End If
End Sub

希望我的代码对你有用 :)


我忘记了:<Dim BACKSPACE As Boolean>。 - Mousa Alfhaily
嘿!我找到了处理粘贴的方法:选择textbox1并转到属性,然后将ShortcutsEnabled设置为“False”,完成 :) [voldemort] (http://stackoverflow.com/users/555690/voldemort) - Mousa Alfhaily

1
请使用这段代码,它会对您有所帮助。
Public Function OnlyDigitsOnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Try

    If System.Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8)        And e.KeyChar <> Microsoft.VisualBasic.Chr(46) Or (InStr(sender.text, ".") > 0 And  e.KeyChar = Microsoft.VisualBasic.Chr(46)) 
    Then
                e.Handled = True
    End If
        Catch ex As Exception
            Common.ErrorHandler(ex)
        End Try
End Function

1
当我需要一个只接受数字的输入框时,通常会使用 NumericUpDown 类。它还可以处理限制和小数位。

0

这里是我写的一些代码。它允许用户删除,如果他们想要的话,用户可以使文本框为空。它处理了用户输入不允许的字符时的情况,还处理了用户将文本粘贴到文本框中的情况。如果用户将一个包含有效和无效字符混合的字符串粘贴到框中,有效字符将出现在文本框中,而无效字符则不会。

它还有逻辑来确保光标正常工作。(设置文本为新值的问题是光标被移回到开头。这段代码跟踪原始位置,并进行调整以解决任何已删除的无效字符。)

这段代码可以放置在任何文本框的TextChaned事件中。请确保将名称从TextBox1更改为与您的文本框匹配。

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    Dim selStart As Integer = TextBox1.SelectionStart
    Dim selMoveLeft As Integer = 0
    Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time.

    For i As Integer = 0 To TextBox1.Text.Length - 1

        If "0123456789".IndexOf(TextBox1.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string.
            newStr = newStr & TextBox1.Text(i)

        ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal.
            selMoveLeft = selMoveLeft + 1

        End If
    Next

    TextBox1.Text = newStr 'Place the new text into the textbox.
    TextBox1.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location.
End Sub

请注意 - 如果您需要对一堆文本框进行此操作,您可以创建一个通用版本的子程序,该子程序接受文本框的引用作为参数。然后您只需要从TextChanged事件中调用该子程序即可。

0
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
    Dim allowedChars As String = "."
    'If allowedChars.IndexOf(e.KeyChar) = -1 Then
    '    ' Invalid Character
    '    e.Handled = True
    'End If
    'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
    '    e.Handled = True
    'End If
    If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False And allowedChars.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
End Sub

你能解释一下为什么这段代码可以解决提问者的问题吗?另外,为什么你在解决方案中留下了注释掉的行? - Kate Gregory

0
Private Sub TMarksTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TMarksTextBox.KeyPress
        If e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接