tags:

views:

44

answers:

2
Private Sub TextBox1_KeyPress(ByVal sender As 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

this code accept only digits and its working like a charm but if i typed a wrong number its not allowing me to use the delete or the backspace on the keyboard how to solve this problem ?

+1  A: 

This goes back a long time, back when terminals were still used. You'll also have to allow the backspace character. Put this in the front of your code:

If e.KeyChar = Chr(8) Then Exit Sub

The Delete key doesn't need any help, that already works.

Hans Passant
A: 

You should have a look at

Keys Enumeration

Keys.Back : The BACKSPACE key.

astander