"Go To Line" in a textbox.

Simple question, can't find the answer.

I can retrieve the line that I want in a RichTextBox control (multiline) and now I want to "go" to that line.  Basically, I've got an RTB that's getting data added to it constantly.  When my "autoscroll" is turned on, I simply set the .SelStart to the end of the document, and that works passingly.  Now I want to be able to turn off the scroll, and have the RTB just sit there.  Unfortunately, every time the data in the RTB is swapped (I have to generate the .rtf myself) the box moves back to the beginning of the "document."

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

learning_t0_pr0gramConnect With a Mentor Commented:
how about..

Dim Tmp() As String

Tmp() = Split(Text1.Text, vbCrLf)
Text1.SelStart = InStr(1, Text1.Text, Tmp(182)) - 1
Text1.SelLength = Len(Tmp(182))
Try this:

RichTextBox1.SelStart = Len(RichTextBox1)

if you are already doing this , then supply more code.  and explain more.

Javin007Author Commented:
I don't really know how much more simply to put it.  I've got a multi-line textbox, I want to "jump to" line 183.  (WITHOUT having to cycle through 183 instr's to trying to set the SelStart past each vbCrLf.  This will not be accepted as an answer.)

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

ArkConnect With a Mentor Commented:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
   Dim idx As Long, LineNumber As Long
   LineNumber = Val(Text2.Text) - 1 'line number is 0-bazed
   idx = SendMessage(Text1.hwnd, EM_LINEINDEX, LineNumber, ByVal 0&)
   Text1.SelStart = idx
End Sub

Private Sub Form_Load()
   Dim s As String
   For i = 1 To 190
      s = s & "This is a line number " & i & vbCrLf
   Next i
   Text1 = Left(s, Len(s) - 2)
   Text2 = 183
   Command1.Caption = "Show line"
End Sub

unknown_routineConnect With a Mentor Commented:
Hi Again,

you can use split function to do the job:

I assume  183 or more items are in the RTB.

Private Sub Command1_Click()

  Dim aText() As String
  Dim i As Long
  Dim lineToFind As Long
  lineToFind = 183
  aText = Split(RichTextBox1.Text, vbCrLf)
  RichTextBox1.Find aText(lineToFind - 1)
  RichTextBox1.SelLength = 0

End Sub
ArkConnect With a Mentor Commented:
Split function is good when all lines are shorter then text box (RTB) width, but if some strings wrapped, this give incorrect result. BTW, here is a code to retrive REAL 183 string text:


Private Sub Command2_Click()
   Dim sLine As String, LineNumber As Long
   Dim lineIndex As Integer, LineLength As Long
   Dim nLines As Long
   nLines = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, ByVal 0&)
   If nLines < Val(Text2.Text) Then
      MsgBox "There are only " & nLines & " lines in a textbox. Can not find line #" & Val(Text2.Text)
      Exit Sub
   End If
   LineNumber = Val(Text2) - 1 '183-1=182
'Get line index (char number from a beginning of text
   lineIndex = SendMessage(Text1.hwnd, EM_LINEINDEX, LineNumber, ByVal 0&)
'Get line length
   LineLength = SendMessage(Text1.hwnd, EM_LINELENGTH, lineIndex, ByVal 0&) + 1
'Prepare string buffer - it should be 2 bytes longer then actual string
   sLine = String(LineLength + 2, 0)
'First 2 bytes are string length
   Mid$(sLine, 1, 1) = Chr$(LineLength And &HFF)
   Mid$(sLine, 2, 1) = Chr$(LineLength \ &H100)
' Get string into buffer
   nRet = SendMessage(Text1.hwnd, EM_GETLINE, LineNumber, ByVal sLine)
   MsgBox "Line No." & Text2 & " = " & sLine
End Sub
David LeeConnect With a Mentor Commented:
If all you want to do is scroll the RichTextBox to a particular line, then I think this should work fine.  I tried it out and it worked ok in my test.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long

Const EM_SCROLL = &HB5

Const SB_LINEUP = 0
Const SB_PAGEUP = 2
Const SB_TOP = 6
Const SB_BOTTOM = 7

Private Sub ScrollToLine(RTB As RichTextBox, intLineNumber)
    Dim intCounter As Integer
    SendMessage RTB.hWnd, EM_SCROLL, SB_TOP, 0      'Go to the top of the box
    For intCounter = 1 To intLineNumber - 1                     'Subtract 1 from the line number since we start on line 1
        SendMessage rtbControlName.hWnd, EM_SCROLL, SB_LINEDOWN, 0
End Sub

I listed the other constants since the allow you to scroll by pages as well as to the bottom of the RTB.  There's also an EM_LINESCROLL message that's supposed to let you go directly to a particular line, but it doesn't seem to work.  Instead of scrolling to a particular line it always scrolls to the bottom of the RTB.
Javin007Author Commented:
Okay, explanation of my point split:

For some bizzare, and unknown reason, the app has STOPPED (with no additional coding or changes of properties that I can find) giving me the problem I was having before.  HOWEVER.

I awarded Ark 200 points because he has helped me in some fashion or another with multiple other issues that he wasn't awarded for.

I awarded 100 to unknown routine for a valiant attempt.

I awarded 100 to BlueDevilFan for an attempt, however, it was similar to another bit of code given in another avenue that didn't work out like I'd wanted.  (visible scrolling issues with each change.)

I awarded 100 to "Learning t0 pr0gram" (despite his attempted 1337 nick) because his option seemed that he'd actually put some thought into it, though it wasn't quite related to my problem...

All Courses

From novice to tech pro — start learning today.