Validating TextBox Inputs

Hello from Seattle,

I am between beginner and intermediate level in VB.Net 2010. I am trying to validate TextBox input to accept ONLY "A", "a", "S", "s" or the Backspace key. I am missing something and getting a bit frustrated.

My code is:

Private Sub txtAnswer_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _Handles txtAnswer.KeyPress

'Accept only A, a, S, s or Backspace key for "txtAnswer" TextBox Control input
Dim KeyAscii As Integer = Asc(e.KeyChar)

If ((KeyAscii = 8) Or (KeyAscii = 65) Or (KeyAscii = 83) Or (KeyAscii = 97) Or (KeyAscii = 115)) Then
e.Handled = True
KeyAscii = 0
Else
e.Handled = False
End If
End Sub

I am sure a fresh look tomorrow will make the answer pop, but if anyone has the answer, please enlighten me.

Thank you, Henry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories