[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?
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
Re: Step through lines in an ActiveX multi-line textbox
Re: Step through lines in an ActiveX multi-line textbox
Thanks, but the first one is vb.Net and I need to use VBA and it looks like the second one is not for an ActiveX textbox.
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/
Re: Step through lines in an ActiveX multi-line textbox
Quote:
Originally Posted by
MartinLiss
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
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.
Quote:
Need to set the SelStart as well. It needs to know where to start the selection.
Good point but how do I do that?
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.
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
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
Re: Step through lines in an ActiveX multi-line textbox