How do i avoid the entry of Numbers in a textbox?

Recommended Answers

All 11 Replies

Use the following handler

Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Char.IsDigit(e.KeyChar) Then
        e.Handled = True
    End If
End Sub
 Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
     'accepts only numbers
     If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
         e.Handled = True
     End If
     If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
         e.Handled = False
     End If
 End Sub
commented: does the opposite of what was asked -2

inside your textbox code go to the keypress event and paste the code of reverend jim... ^_^

IsDigit code will allow to enter everything apart from digits....

  'accepts only alphabets
          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
               e.Handled = True
          End If
          If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
               e.Handled = False
          End If
commented: unnecessarily complicated and has redundant code +0

IsDigit code will allow to enter everything apart from digits....

So put "Not" in front of it. If Not Char.IsDigit(e.KeyChar) Then

The "Char.XXx" functions will account for regional language settings that use unicode characters outside the range of those used for English letters.

The OP wants to enter anything EXCEPT digits. poojavb then posts code that accepts ONLY digits, then posts something much more complex than is required and now we have a suggestion to use "Not Char.IsDigit". I fail to understand why this is so confusing.

Here's the keypress code for your textbox.. you must use keypress event in your textbox.

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

        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
        If Asc(e.KeyChar) = 8 Then
            e.Handled = False
        End If
    End Sub
commented: does the opposite of what was asked -2

brylle - exactly how does your code accomplish what was asked? In English it becomes

If char is not a digit Then
    ignore this keypress
End If

If char is backspace Then
    don't ignore this keypress
End If

What you have posted does the complete opposite of what was asked. If you want code that is even simpler than what I already posted then you can use

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

I think the only way to simplify this further is

Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    e.Handled = Char.IsDigit(e.KeyChar)
End Sub

Again, I have to ask, why are you making this so complicated? The question, simply stated, is "how can I ignore digits in a textbox"

e.Handled = Char.IsDigit(e.KeyChar)

does that.

Sorry about that.. here's the code..

You're code is still wrong, the OP said nothing about restricting symbols... Please stop, a quick, clean, readable and correct solution has already been offered; your solutions offer very little of that.

I had originally deleted brylle's last post because the posted code was so (let's be gracious) wide of the mark. Upon reflection I decided to undelete it and go through it with comments as to why it is bad. I do this not to be nasty but in the hope that I can break a few bad programming habits. I find that no matter how long I have been programming, someone, somewhere will look at my code and say, "you know there is a much easier way to do that".

So let's look at the code and see what shouldn't be done.

If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 90 And Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then
    If Asc(e.KeyChar) <> 32 Then
        e.Handled = True
    End If
End If

If Asc(e.KeyChar) = 8 Then
    e.Handled = False
End If

Asc(e.KeyChar) < 65 etc

First of all, each subexpression is needlessly complex. He first converts an ASCII character to a number then compares that number to another number which is a literal that carries no intrinsic meaning. If you want to compare apples to apples then do it. Don't convert both to oranges and then compare those. Better to do

If e.KeyChar < "A"

The intent is pretty much self-evident. Next, never mix multiple ANDS and ORS, etc without using parentheses to force precendence. In brylle's example, the expression

If a Or b And c Or d

is actually evaluated as

If a Or (b And c) Or d

which is not what was intended. Even if it was evaluated as intended it should still be written with parentheses to make the intent clear. In any case his intent seems to be to check if the entered char is an upper or lower case letter. His code does not do that.

If Asc(e.KeyChar) <> 32 Then

I have no idea what this is doing there. It's checking for a blank.

If Asc(e.KeyChar) = 8 Then

Same here except the test is for backspace.

Last but not least, after possibly setting e.Handled to True in the first block, he then adds another block that says "maybe not".

As I have already posted, the solution is one line. I don't evan want to think about what kind of code brylle writes if there is anything complex to be accomplished.

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.