Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


"Go To Line" in a textbox.

Posted on 2004-03-23
Medium Priority
Last Modified: 2010-05-18
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."

Question by:Javin007
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 15

Expert Comment

ID: 10663276
Try this:

RichTextBox1.SelStart = Len(RichTextBox1)

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


Author Comment

ID: 10663352
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.)

LVL 28

Assisted Solution

Ark earned 800 total points
ID: 10663716
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Assisted Solution

unknown_routine earned 400 total points
ID: 10663734
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
LVL 28

Assisted Solution

Ark earned 800 total points
ID: 10663817
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
LVL 76

Assisted Solution

by:David Lee
David Lee earned 400 total points
ID: 10664249
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.

Accepted Solution

learning_t0_pr0gram earned 400 total points
ID: 10664327
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))

Author Comment

ID: 10665113
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...


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question