2011-11-21 90 views
2

我製作了一個文本框,它僅將字符使用限制爲數字和句點。很好,但現在我不能進入退格鍵來修改我的文本框中輸入的任何數據。我怎樣才能解決這個問題?文本框不允許按退格鍵?

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 

回答

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?

相關問題