文本框不允许按下退格键?

时间:2011-11-21 07:47:30

标签: vb.net textbox key

我制作了一个文本框,它将字符使用限制为仅限数字和句点。很好,但现在我无法输入退格键来修改我的文本框中输入的任何数据。我该如何解决这个问题?

    Private Sub TextBox10_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox10.KeyPress

    Dim allowedChars As String = "1234567890."

    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If

End Sub

1 个答案:

答案 0 :(得分:6)

您可以使用

测试退格
If e.KeyChar = ChrW(8) Then
    MessageBox.Show("backspace!")
End If

所以你的整个代码将成为:

Private Sub TextBox10_KeyPress(ByVal sender As Object, ByVal e As     System.Windows.Forms.KeyPressEventArgs) Handles TextBox10.KeyPress

    Dim allowedChars As String = "1234567890."

    If allowedChars.IndexOf(e.KeyChar) = -1 andalso
            Not e.KeyChar = ChrW(8) Then
        ' Invalid Character
        e.Handled = True
    End If

End Sub

类似的问题:How can I accept the backspace key in the keypress event?