[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-02
14
Medium Priority
?
1,294 Views
Last Modified: 2012-06-22
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
Comment
Question by:Boon01
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 60 total points
ID: 19998367
Why not use the on exit event, using IsNumeric?   Much simpler...
0
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 255 total points
ID: 19998375
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
 
LVL 1

Author Comment

by:Boon01
ID: 19998426
"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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998446
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19998450
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
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 60 total points
ID: 19998475
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998504
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998509
Chris_bottomley has it best. Simple and easy to read.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998526
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998550
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
 
LVL 1

Author Comment

by:Boon01
ID: 19998595
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
 
LVL 1

Author Comment

by:Boon01
ID: 19998857
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19998940
I tried it, seems to work great.
0
 
LVL 1

Author Comment

by:Boon01
ID: 19999285
Thanks for all the help!!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question