Solved

"Go To Line" in a textbox.

Posted on 2004-03-23
8
1,140 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now