dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Prefix on left of textbox text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2017
    Posts
    79

    Resolved [RESOLVED] Prefix on left of textbox text

    Originally posted at #7 in link bellow
    http://www.vbforums.com/showthread.p...gn-in-Text-Box

    Modified a little but not exactly what i want. How to do it correctly

    Code:
    Private Sub Text1_Change()
        Dim Selstart As Integer
        Dim Text As String
        
        Selstart = Text1.Selstart
        Text = Text1.Text
        If Left$(Text, 7) <> "Km From" Then
            Text1.Text = Replace$("Km From", "", Text)
            Text1.Selstart = Selstart
        End If
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,187

    Re: Prefix on left of textbox text

    Not perfect, but it could work for what you want:
    Code:
    Private Sub Text1_Change()
        Dim Selstart As Integer
        Dim Text As String
        Dim Prefix As String
        Dim LenPrefix As Long
        Dim c As Long
        
        Prefix = "Km From"
        If Right(Prefix, 1) <> " " Then
            Prefix = Prefix & " "
        End If
        LenPrefix = Len(Prefix)
        
        Selstart = Text1.Selstart
        Text = Text1.Text
        If Left$(Text, LenPrefix) <> Prefix Then
            For c = LenPrefix - 1 To 1 Step -1
                If Left$(Text, c) = Left(Prefix, c) Then
                    Text = Mid(Text, c + 1)
                    Exit For
                End If
            Next c
            Text1.Text = Prefix & Replace$(Text, "Km From", "")
            If Selstart >= (LenPrefix + 1) Then
                Text1.Selstart = Selstart
            Else
                Text1.Selstart = LenPrefix + 1
            End If
        End If
    End Sub
    Last edited by Eduardo-; Dec 6th, 2017 at 08:10 AM.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,520

    Re: Prefix on left of textbox text

    How to do WHAT correctly? If you want the 'words' "Km From" on the left of the textbox, your code does indeed do that. BUT, I would definitely look at using a label placed to the left of your textbox instead. Keep it simple for the user.

    If the textbox is filled from an external source (database, file, etc), then you could simply change the text going into the textbox by adding "Km from " when doing so. But, if you are having users TYPE in the box (the main purpose of textboxes over labels), then I would not try to confuse anyone, simply use a label to the left.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2017
    Posts
    79

    Re: Prefix on left of textbox text

    Thank you Eduardo. Solved the problem.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,520

    Re: [RESOLVED] Prefix on left of textbox text

    I'd hate to be your client....

    Anyway, glad you are satisfied and marked this resolved.

    Good day,

    Sammi

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width