How to disable numeric characters?

marksquall

Active member
Joined
Oct 17, 2009
Messages
28
Programming Experience
3-5
Dear Visual Basic .NET Forums administrators and members,

A greeting of peace. I hope everyone is doing fine upon reading this forum.

Well, it's really my first time to try Visual Basic .NET, then I realized that not all my previous experience with VB6 will work smoothly in VB.NET. I am creating a simple program that will enter a name in the TextBox and will greet them by using the MessageBox class. But I wonder, since the TextBox that I'll be using is for inputting a name, I want to "trap" all numeric keys in my keyboard, I just don't know how to do it. This is fair easy in VB6, using the Text1_KeyPress(KeyAscii As Integer) subroutine.

Thank you and more power to all administrators and members. God bless. :)


Respectfully yours,

MarkSquall
 
At its most basic...this will get you started
VB.NET:
   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Char.IsNumber(e.KeyChar) = True Then
            MessageBox.Show("Letters Only")
            TextBox1.Text = String.Empty
        End If
    End Sub
 
It's better late than never to say "thanks"...

Dear Mr. Hack,


I do hope it's not too late to say thank you for the knowledge you shared. More power to you and to all members of this site. :D



Warm regards,


MarkSquall
 
Even better -- the following will simply not allow any characters other than letters to be entered, unless it's the backspace key (allowing user to make corrections) or the Enter key, when input is complete. It won't just erase the text that's already there:


VB.NET:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsLetter(e.KeyChar) Then e.Handled = True  'letters only
        If e.KeyChar = Chr(8) Or e.KeyChar = Chr(13) Then e.Handled = False  'Backspace or Enter
    End Sub
 
Back
Top