"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."

-Javin
LVL 4
Javin007Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
learning_t0_pr0gramConnect With a Mentor Commented:
how about..

Dim Tmp() As String

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

RichTextBox1.SelStart = Len(RichTextBox1)

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

0
 
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.)

-Javin
0
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
Const EM_LINEINDEX = &HBB

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
   Text1.SetFocus
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

Regards
Ark
0
 
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.SetFocus
  RichTextBox1.SelLength = 0

End Sub
0
 
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:

Const EM_LINELENGTH = &HC1
Const EM_GETLINE = &HC4
Const EM_GETLINECOUNT = &HBA

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
0
 
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 EM_LINESCROLL = &HB6

Const SB_LINEUP = 0
Const SB_LINEDOWN = 1
Const SB_PAGEUP = 2
Const SB_PAGEDOWN = 3
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
    Next
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.
0
 
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...

-Javin
0
All Courses

From novice to tech pro — start learning today.