Results 1 to 4 of 4

Thread: Add text in text block while typing

  1. #1

    Thread Starter
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314

    Question


    Check this one out.

    I would like my program to auto add a dash while typing in a Social Security Number.

    Example: User types in "1 2 3" and then my program inserts the first - (dash) user then types in "4 5" and my program inserts the last - (dash).

    I tried using the code below but it places my cursor at the start of the text box afterward.

    'Private Sub TextSSN_KeyUp(KeyCode As Integer, Shift As Integer)

    'If Len(TextSSN.Text) = 3 Then TextSSN = TextSSN & "-"
    'If Len(TextSSN.Text) = 6 Then TextSSN = TextSSN & "-"

    'End Sub

    Do you have an idea on how this can be done and make the cursor go to the end of the text line?

    Thanks for reading this.
    Steve Stunning

  2. #2
    Guest
    Try this.
    Code:
    Private Sub TextSSN_KeyUp(KeyCode As Integer, Shift As Integer)
        If Len(TextSSN.Text) = 3 Then TextSSN = TextSSN & "-"
        If Len(TextSSN.Text) = 6 Then TextSSN = TextSSN & "-"
        TextSSN.SelStart = Len(TextSSN)
    End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Have you considered using the MaskEdit control instead of a regular TextBox?
    If you want to go with a TextBox use this code:
    Code:
    Private Sub TextSSN_KeyUp(KeyCode As Integer, Shift As Integer) 
        If Len(TextSSN.Text) = 3 Then 
            TextSSN = TextSSN & "-" 
            TextSSN.SelStart = Len(TextSSN.Text)
        End If
    End Sub
    Good luck!

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Thumbs up Beat me to the punch...

    Man, you guys are too fast. By the way, I agree with Joacim about the masked edit control. However, I would alter his code a bit to include a length of 6 for the text box.
    Code:
    Private Sub TextSSN_KeyUp(KeyCode As Integer, Shift As Integer) 
        If Len(TextSSN) = 3 or Len(TextSSN) = 6 Then 
            TextSSN = TextSSN & "-" 
            TextSSN.SelStart = Len(TextSSN.Text)
        End If
    End Sub
    All the best.

    [Edited by OneSource on 08-22-2000 at 11:38 AM]

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