Solved

"Go To Line" in a textbox.

Posted on 2004-03-23
8
1,142 Views
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."

-Javin
0
Comment
Question by:Javin007
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10663276
Try this:

RichTextBox1.SelStart = Len(RichTextBox1)

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

0
 
LVL 4

Author Comment

by:Javin007
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.)

-Javin
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 200 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 15

Assisted Solution

by:unknown_routine
unknown_routine earned 100 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.SetFocus
  RichTextBox1.SelLength = 0

End Sub
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 200 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:

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
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 100 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 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
 
LVL 4

Accepted Solution

by:
learning_t0_pr0gram earned 100 total points
ID: 10664327
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
 
LVL 4

Author Comment

by:Javin007
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...

-Javin
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

785 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