PDA

Click to See Complete Forum and Search --> : Text box question


hlieu
Aug 15th, 2000, 12:54 PM
Hi,

My question is: How to tell the different between a character(A-Z)and a number(1-0) that has enter the textbox.

Example: I have text1 which allows ONLY numbers to be enter. If the user enters character, it'll show appropriate message.

Thanks

JHausmann
Aug 15th, 2000, 01:18 PM
Following function should be called from the keypress event in your text field.


Public Function CheckKeyPress(nASCIIValueofKey As Integer) As Boolean

CheckKeyPress = True

If nASCIIValueofKey < Asc("0") Or nASCIIValueofKey > Asc("9") Or nASCIIValueofKey = 8 Then
'nothing, we will allow these characters
Else 'the user has pressed some key other than 0-9 or backspace
nASCIIValueofKey = 0 ' cancel the character
Beep
CheckKeyPress = False
End If

End Function

'place this in your keypress event for "textfield"
Dim wasKeygood As Boolean

wasKeygood = CheckKeyPress(KeyAscii)
If Not wasKeygood Then
KeyAscii = 0
textfield.SelStart = 0
textfield.SelLength = Len(textfield)
Exit Sub
End If