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?

Recommended Answers

All 4 Replies

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

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.