【问题标题】:textbox does not allow for backspace to be pressed?文本框不允许按下退格键?
【发布时间】:2011-11-21 07:47:30
【问题描述】:

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

    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

【问题讨论】:

    标签: vb.net textbox key


    【解决方案1】:

    您可以通过使用来测试退格

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多