Results 1 to 11 of 11

Thread: [RESOLVED] Step through lines in an ActiveX multi-line textbox

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] Step through lines in an ActiveX multi-line textbox

    I have an ActiveX multi-line textbox in a userform. The lines of the textbox are separated by vbCr and are not all the same width.

    Using the keyboard up and down arrows I'd like to select and highlight each line. Can someone help?

  2. #2

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Step through lines in an ActiveX multi-line textbox

    When using the down arrow in the textbox's MouseDown event this steps through the lines but doesn't highlight them. txtHits is the name of the textbox.
    Code:
    Dim strLines() As String
    Select Case KeyCode
        Case vbKeyDown
        With txtHits
            strLines = Split(txtHits.Text, vbCr)
            txtHits.SelLength = Len(strLines(txtHits.CurLine))
        End With
    End Select

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Step through lines in an ActiveX multi-line textbox


  4. #4

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Step through lines in an ActiveX multi-line textbox

    Those might give you some ideas as to what to try, however. The same principles should apply.

    More links:

    https://www.vbforums.com/showthread....Copy-that-line

    https://chandoo.org/forum/threads/ne...text-box.1603/

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Step through lines in an ActiveX multi-line textbox

    Quote Originally Posted by MartinLiss View Post
    When using the down arrow in the textbox's MouseDown event this steps through the lines but doesn't highlight them. txtHits is the name of the textbox.
    Code:
    Dim strLines() As String
    Select Case KeyCode
        Case vbKeyDown
        With txtHits
            strLines = Split(txtHits.Text, vbCr)
            txtHits.SelLength = Len(strLines(txtHits.CurLine))
        End With
    End Select
    Need to set the SelStart as well. It needs to know where to start the selection.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Step through lines in an ActiveX multi-line textbox

    I'm sorry but I made a mistake in my original question; the code is in the txtHits_KeyDown event.

    Need to set the SelStart as well. It needs to know where to start the selection.
    Good point but how do I do that?

  8. #8
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Step through lines in an ActiveX multi-line textbox

    You can get the contents of the text box, split the lines into a string array, then you can use the InStr function to get the starting position of that string in the complete text, and the Len function to get the length, and then use those to set the part that you want highlighted.

  9. #9

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Step through lines in an ActiveX multi-line textbox

    Here's my current code but it doesn't work properly. After I manually select the first line in the textbox and then press the down-arrow, the selection goes to the 2nd character of the 4th(!) line and if I press it again it goes to the 4th character of the 7th line, and nothing is highlighted in either case.
    Code:
        Dim intPos As Integer
        Dim intCurLine As Integer
        With txtHits
            ' Split the textbox's text into individual lines
            strLines = Split(txtHits.Text, vbCr)
            ' Store the current line
            intCurLine = txtHits.CurLine
            ' Find the starting position of the next line which is the oneto be highlighted
            intPos = InStr(txtHits.Text, strLines(intCurLine + 1))
            ' Set SelStart to that position
            .SelStart = intPos
            ' Set the SelLength to the length of the next line
            .SelLength = Len(strLines(intCurLine + 1))
            'Debug.Print "Current line: " & intCurLine & " SelStart: "; .SelStart & " SelLength: " & .SelLength
        End With

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Step through lines in an ActiveX multi-line textbox

    First draft (UserForm with a Textbox)
    Not tested for a single line

    Note: SelStart RETURNS the Position of the Cursor if no Text is selected, with Pos=0 being left of the first character, Pos=1 right of the first character etc.

    Code:
    Private sText As String
    Private aText() As String
    Private aPos() As Long
    
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim i As Long
    Dim j As Long
        If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Then
            j = 0
            For i = 0 To UBound(aPos)
                If TextBox1.SelStart >= aPos(i) Then
                    j = aPos(i)
                Else
                    Exit For
                End If
            Next
            TextBox1.SelStart = j
            TextBox1.SelLength = Len(aText(i))
        End If
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim i As Long
        sText = "this is a test" & vbCrLf & "with multiple lines" & vbCrLf & "this is the third line"
        aText = Split(sText, vbCrLf)
        ReDim aPos(0 To UBound(aText) - 1)
        aPos(0) = Len(aText(0)) + 1
        If UBound(aPos) > 0 Then
            For i = 1 To UBound(aPos)
                aPos(i) = aPos(i - 1) + Len(aText(i)) + 1
            Next
        End If
        TextBox1.Text = sText
        TextBox1.SelStart = 0
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11

Tags for this Thread

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