in a textbox, users should input numbers and if letters are inputted, it will create an error that only numbers are accepted. please help me. i am using vb6 and windows xp. thanks.
how to trap this?
Collapse
X
-
you could catch the ascii code of the entered character and then remove the last character entered if it is not within the right values.
I have got some VB .net code to do this that I have worked out but I would rather you tried to do this yourselft and post any code that you are having problems with as it does not help you if I post the full code
if it is numeric you might also be able to use ' isnumeric ' rather than actually checking the ascii code -
There is actually a code that you can place in the keypress event for the text box that will limit it to only accepting numbers, delete and backspace. As jg007 said... attempt to work it out and let us know where you get and we can help you more from there. Or show us what you already have that you're trying to use to do so.Comment
-
Private Sub BurnText1_KeyUp (KeyCode As Integer, Shift As Integer)
If (KeyCode >= 48 And KeyCode <= 57) Or (KeyCode >= 96 _
And KeyCode <= 105) Or (KeyCode = 8) Or (KeyCode = 32) Or (KeyCode = 191) _
Or (KeyCode = 110) Then
Else
BurnText1.Text = ""
End If
End Sub
that's the code i've used and it works like a charm.. now, if you want to trap for letters only to be inputted, you should change the constant values. not all constant values are readily available in vb6 so you have to research for a list of keycode constant values,.
hope this helps.Comment
-
your code is great and will work but as a reminder that there are always plenty of way to do something :) , this could be easily amended to allow letters only although would require extra code to avoid special character
this code also avoids clearing the textbox when invalid characters are entered -
that is the fun thing of programming trying to work out how to achieve a goal and although I just use whatever works for me several months down the line I inevitably find another way to do it
Code:Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not IsNumeric(e.KeyChar) Then e.KeyChar = Nothing End If End Sub
Comment
Comment