Results 1 to 2 of 2

Thread: Text box question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    Edmonton,AB,Canada
    Posts
    28
    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

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Following function should be called from the keypress event in your text field.

    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width