-
Aug 31st, 2022, 01:37 PM
#1
[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?
Last edited by MartinLiss; Aug 31st, 2022 at 01:55 PM.
-
Aug 31st, 2022, 01:48 PM
#2
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
Last edited by MartinLiss; Aug 31st, 2022 at 01:54 PM.
-
Aug 31st, 2022, 02:13 PM
#3
Re: Step through lines in an ActiveX multi-line textbox
-
Aug 31st, 2022, 02:27 PM
#4
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.
-
Aug 31st, 2022, 02:33 PM
#5
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/
-
Aug 31st, 2022, 03:06 PM
#6
Re: Step through lines in an ActiveX multi-line textbox
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
-
Aug 31st, 2022, 03:40 PM
#7
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?
-
Aug 31st, 2022, 03:55 PM
#8
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.
-
Aug 31st, 2022, 05:08 PM
#9
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
-
Sep 1st, 2022, 01:40 AM
#10
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
-
Sep 1st, 2022, 10:54 AM
#11
Re: Step through lines in an ActiveX multi-line textbox
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|