Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enforcing numeric chars in textbox widget.

Status
Not open for further replies.

jerryk

Programmer
Jun 13, 2001
82
0
0
US
Hi,
I'm an old programmer, new to VB.

I have a TextBox but want the user to only enter integers (I suppose decimals are ok)

I've set the datatype to integer, but the Change() event crashes at runtime giving me "type mismatch" errors when I enter a-z characters.

Is there any way to make the textbox only accept numbers?

many thanks,
Jerry
 
Code:
Private Sub Number_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
            Handles txtAmount.KeyPress, txtOrccNumber.KeyPress
        Select Case e.KeyChar
            Case Convert.ToChar(8) ' Backspace
            Case "0" To "9"
            Case Else
                e.Handled = True
        End Select
    End Sub

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thanks,

I was kinda close with:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub

Your code helps a lot!!!
 
JohnYingling,
You shouldn't reject keys with an ANSI value of 31 or less. These include important keys such as Backspace, Escape, Tab, and Enter. I belive you're only accouting for the BackSpace key in your code above.

JC

Oh Wow!!! I was unaware of all the smileys available here at tek-tips.
[rockband]
 
That is correct. Tab key is already intercepted by VB. Enter and Esc do nothing and so are of no importance. Others like ctrl-c can be handled if you so wish. Where is it written "You shouldn't reject keys with an ANSI value of 31 or less?"

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
JerryK.

You need to validate the textbox contents even with the KeyPress "intervention" since the user is free to right-click on the textbox to bring up a Context Menu where alpha data can be pasted.
In TextChanged, at least protect yourself.
if txt.TextLength = 0 then exit sub
Try
intw = Integer.Parse(txt.text)
catch
end sub ' or whatever
End Try
'''we have a number

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
try this thread

thread796-868344

Christiaan Baes
Belgium

What a wonderfull world - Louis armstrong
 
Thanks for all your help. Once I knew the proper keywords: keypress etc, I was able to find similar threads.

Thanks again,

Jerry
 
JohnYingling,
JohnYingling said:
Where is it written "You shouldn't reject keys with an ANSI value of 31 or less?"
The answer to that is Francesco Balanea's book "Programming Visual Basic 6.0", from Microsoft Press, 1999. It's in the beginning of the third chapter in the discussion about Intrinsic controls.


The reason why you shouldn't reject keys with Ansi values less than 32 it's not that it's a bad programming practice. It's just that you may want to respond to them. For example I have an application where the user enters something in a textbox and presses the Enter key or click on a button for the program to process his entry. So, in my case, as opposed to what you said, the Enter key is indeed imporant.

In any case, my statement is probably missing something. I should have said "You should not reject keys with Ansi value less than 32 if you plan to respond to them..." or something like that. Many times it'll be OK to reject them. But there will be times, like in my case, where it won't be OK, and since taking care of them doesn't hurt, then the bottom line should be that you shouldn't reject them.

JC

Oh Wow!!! I was unaware of all the smileys available here at tek-tips.
[rockband]
 
The problem I see is if someone keys in a plus sign accidently instead of a 6. They are right next to each other. If your validation simply omits the plus sign, then the user may not be aware that he or she made a typo unless he or she is watching the screen while typing.

For example, consider a figure "562345". The user keys in "5+2345". If the user does not pay close enough attention, then "52345" may be entered into your system.

Consider using an ErrorProvider. This is similar to web site forms you see that don't let you submit the form unless it is filled out properly. Here is a link:


Otherwise, if you don't want to look into that, I would allow for special characters such as enter, etc. Go to to get an idea of what you need to account for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top