-2

i am trying to set a key press event to accept only letters a-z and back space no numbers but im not sure how to word it can anyone help?

5
Contributors
4
Replies
18
Views
8 Years
Discussion Span
Last Post by __avd
0

you just to handle keychar, but you must know the ascii of letters. this following code will not allowed you to input other character except letters or strings. so u cannot input number or other special character.
try this following code :

Private Sub textDate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textDate.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
             Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
             And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
             Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            
            e.Handled = False
        End If
    End Sub
0

This post really helped me. I have another question, continuation to before thread. I dont how to code for textbox keypress, where i want user to enter only numbers (e.g. 0009, 0010.. so on..) and deny rest.

Please help..!!

TashiDuks

0

Hi,

Try this:

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

If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then

e.Handled = True

End If

End Sub

Or this:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
as System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub

Edited by Luc001: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.