Results 1 to 9 of 9

Thread: Simple (or not) text box question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Angry

    Two things I'm trying to do:

    Limit input to digits 0-9 and

    on the 2nd number, add a hyphen so the user won't have to type.

    My hopefully end result would be 34-123.

    Any help would be appreciated.

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    No Problem
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr(KeyAscii)) Then
    
        KeyAscii = 0
    
    ElseIf Len(Text1.Text) = 2 Then
    
        Text1.Text = Text1.Text & "-"
        Text1.SelStart = 3
        
    End If
    
    End Sub

  3. #3
    Guest
    Try this.
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
        If Len(Text1) = 2 Then Text1.SelText = "-"
    End Sub

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Talking

    Thanks guys. It works just like I needed.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Question

    Sorry to be a pain but what if I the backspace key to be active? Better yet, is there a chart of KEYASCII codes so I can know what's what?

  6. #6
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    the backspace key is 8

    so enclose everything in the Keypress event in an If Not (KeyAscii = 8) Then ... Endif block.

    If you want to get different keyasciis the easiest way is to add another textbox to your form and add this

    Code:
    Private Sub Text2_KeyPress(KeyAscii As Integer)
    Text2.Text = KeyAscii
    KeyAscii=0
    End Sub
    then run your program , put the curser in the textbox and press the key you want, the correct code will apear in the textbox

  7. #7
    Guest
    James Stanich,
    If you are looking for a chart,
    MSDN has topics under Character Sets. It lists the charcters along with their charcter codes.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Thumbs up

    Neat trick. Thanks.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Or even better; use the constant values:
    • vbKeyBack
    • vbKeyReturn
    • vbKeyEscape
    • vbKeyA
    • vbKeyB
    • ...
    • vbKeyF1
    • vbKeyF2
    • ...
    And so on...

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