bring a richtextbox line to the top of the view
i'd like to bring a line of text to the of the RTB.
specifically, i'm using the RTB's find method, and would like the line that has the found selection moved to the top of the RTB. no text manipulation, just manipulation of which section of text is in view of the RTB.
Re: bring a richtextbox line to the top of the view
You can send the EM_LINESCROLL message to the RTB to do this. I wrapped it up in a simple Sub called SetTopIndex to which you pass a reference to the RichTextBox and the zero based line number you want to scroll to. You can get the line number by calling the GetLineFromChar method of the RTB.
VB Code:
Private Declare Function SendMessage _
Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const EM_GETFIRSTVISIBLELINE As Long = &HCE
Private Const EM_LINESCROLL As Long = &HB6
Public Sub SetTopIndex(rtb As RichTextBox, ByVal nLine As Long)
Dim nIndex As Long
nIndex = nLine - SendMessage(rtb.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
Call SendMessage(rtb.hwnd, EM_LINESCROLL, 0&, ByVal nIndex)
End Sub
Re: bring a richtextbox line to the top of the view
You may also want to look at this project from PSC.
It also has methods for getting the line numbers.
Look for this sub: "SetScrollPos"
http://www.planet-source-code.com/vb...34340&lngWId=1
Re: bring a richtextbox line to the top of the view
well, something is wrong here. the RTB is scrolling to the last line and putting that on top. even if i manually put in a value, such as below
VB Code:
SendMessage RichTextBox1.hwnd, EM_LINESCROLL, 0&, 10
it still scrolls to the last line
Re: bring a richtextbox line to the top of the view
You must pass the lParam argument ByVal, and it will scroll that many lines from the current position. Did you try my Sub that calculates the number of lines you have to scroll to get to the line you want?
Re: bring a richtextbox line to the top of the view
wow, my fault. i really didn't think getting rid of ByVal would make a difference!
now then, the easiest way to get the top line at the top? ie, scrolled all the way up.
Re: bring a richtextbox line to the top of the view
Quote:
Originally Posted by dis1411
now then, the easiest way to get the top line at the top? ie, scrolled all the way up.
What do you mean? Do you want to know which line there is at the top? If that is the case just send the EM_GETFIRSTVISIBLELINE message (also shown in my first example).
Re: bring a richtextbox line to the top of the view
well, EM_LINESCROLL scrolls down, but i also need to scroll all the way up to the beginning of the document
Re: bring a richtextbox line to the top of the view
If you use my sub just call it with 0 as the line number.
VB Code:
Call SetTopIndex(RichTextBox1, 0)
Or if you want to write your own code just pass a negative number in lParam when you send the EM_LINESCROLL to scroll upwards. The number of lines you need to scroll up can be retrieved by sending EM_GETFIRSTVISIBLELINE.
Re: bring a richtextbox line to the top of the view
jeez, i posted too soon again. you can also do rtb.selstart = 0. in my case i accidentally did something after that which cancelled the effect
Re: bring a richtextbox line to the top of the view
Quote:
Originally Posted by dis1411
jeez, i posted too soon again. you can also do rtb.selstart = 0. in my case i accidentally did something after that which cancelled the effect
That's true. But that will also change the caret position but maybe you want to do that.
Re: bring a richtextbox line to the top of the view
for my purposes, it's no matter. thanks