• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1300
  • Last Modified:

Only allow numbers to be entered in text box...

Hello all, I have a text box that I only want the user to be able to enter number type entries. I want them to be able to enter 1, -1, or 1.1 but not one. I have this right now but it doesn't allow me to enter a decimal or a neg sign.....Can anyone tell me how I can use this code or some other keypress code to limit the user imput?

Protected Sub textbox_OnKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress

        'Validate that the only thing the user can enter is a number
        If [Char].IsNumber(e.KeyChar) OrElse AscW(e.KeyChar) = CInt(Keys.Back) Then
            e.Handled = False
            Return
        End If
        e.Handled = True
    End Sub
0
Boon01
Asked:
Boon01
  • 7
  • 4
  • 2
  • +1
3 Solutions
 
Patrick MatthewsCommented:
Why not use the on exit event, using IsNumeric?   Much simpler...
0
 
Anthony2000Commented:
Have you tried adding an extra test to your if statement and adding the specific "-" and "." to the list. That should solve your problem.
0
 
Boon01Author Commented:
"Why not use the on exit event, using IsNumeric?   Much simpler..."

Would this stop someone from being able to enter alpha or special characters? (ABC ?><)

"Have you tried adding an extra test to your if statement and adding the specific "-" and "." to the list. That should solve your problem."
How would I do this?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Anthony2000Commented:
And as metthewspatric suggested you will need to do some checking in the on exit event to make sure for example that the user didn't enter "----1" or .....1, etc
0
 
Patrick MatthewsCommented:
Boon01 said:
>>"Why not use the on exit event, using IsNumeric?   Much simpler..."
>>
>>Would this stop someone from being able to enter alpha or special characters? (ABC ?><)

People would be able to enter such characters, but then the code would force people to go
back and change it.
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
How about

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not Chr(KeyAscii) Like "[0-9.-]" Then KeyAscii = 0
End Sub

Chris
0
 
Anthony2000Commented:
I made a simple change to your if statement:

      If [Char].IsNumber(e.KeyChar) OrElse AscW(e.KeyChar) = CInt(Keys.Back) OrElse AscW(e.KeyChar) = AscW("-") OrElse AscW(e.KeyChar) = AscW(".") Then
0
 
Anthony2000Commented:
Chris_bottomley has it best. Simple and easy to read.
0
 
Anthony2000Commented:
hhmm, I tried the examle Chris gave and it does not compile under VB.NET 2003, Chris do you know the syntax for VB.NET?
0
 
Anthony2000Commented:
Here it is modified for VB.NET

      If Not Chr(AscW(e.KeyChar)) Like "[0-9.-]" Then
         e.Handled = True
      Else
         e.Handled = False
      End If
0
 
Boon01Author Commented:
matthewspatrick, do you happen to have an example of what how to validate on exit that you suggested?

thanks Anthony2000 your code mod worked great. I just have to do what mathewspatrick suggested too because now the code allows (...) or (---) which give a big fat error when saving to the database.
0
 
Boon01Author Commented:
Alright, this is what I have so far....(I took out the Neg sign-turns out I didn't need it)

Protected Sub textbox_OnKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress

        'Validate that the only thing the user can enter is a number
        If (Char.IsDigit(e.KeyChar) _
        Or e.KeyChar = "." And InStr(Me.textbox.Text, ".") = False _
        Or e.KeyChar = Chr(Keys.Back)) Then
            e.Handled = False
            Return
        End If
        e.Handled = True
    End Sub

I think this will work, unless you guys see something that may cause me issues.
0
 
Anthony2000Commented:
I tried it, seems to work great.
0
 
Boon01Author Commented:
Thanks for all the help!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now