hello,
I want to enter only numbers or only alphabets in my text box in vb.net.
wat code should i use in the keypress event to solve this problem

Like in vb 6 we use key ascii what is the similar thing in vb.NET

PLS help its very imp and urgent

thank you

Recommended Answers

All 4 Replies

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim x As String = e.KeyChar.ToString
        Select Case x
            Case "a" To "z", " ", "0" To "9", "A" To "Z"
                '            'allows these keystrokes thru disallows all others
            Case Else
                e.Handled = True
        End Select
    End Sub

thanks a lot for ur help.
Everything is working fine but have a problem the back space key is also not working when we put this code.
How to make the backspace key work..
Pls reply asap
thankin you

please insert the if stat.. in code

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim x As String = e.KeyChar.ToString

If Asc(e.KeyChar) = 8 Then
Return
End If


Select Case x
Case "a" To "z", " ", "0" To "9", "A" To "Z"
' 'allows these keystrokes thru disallows all others
Case Else
e.Handled = True
End Select
End Sub

Sorry it took so long replying but my computer went down, bummer.
just add chr(8) at the end of the string
Case "a" To "z", " ", "0" To "9", "A" To "Z", Chr(8)

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.