|
-
May 26th, 2005, 11:50 AM
#1
Thread Starter
Frenzied Member
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.
-
May 26th, 2005, 01:31 PM
#2
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
-
May 26th, 2005, 02:21 PM
#3
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
-
May 26th, 2005, 02:46 PM
#4
Thread Starter
Frenzied Member
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
-
May 26th, 2005, 02:55 PM
#5
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?
-
May 26th, 2005, 03:09 PM
#6
Thread Starter
Frenzied Member
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.
-
May 26th, 2005, 03:12 PM
#7
Re: bring a richtextbox line to the top of the view
 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).
-
May 26th, 2005, 03:13 PM
#8
Thread Starter
Frenzied Member
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
Last edited by dis1411; May 26th, 2005 at 03:20 PM.
-
May 26th, 2005, 03:21 PM
#9
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.
-
May 26th, 2005, 03:25 PM
#10
Thread Starter
Frenzied Member
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
-
May 26th, 2005, 03:26 PM
#11
Re: bring a richtextbox line to the top of the view
 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.
-
May 26th, 2005, 03:34 PM
#12
Thread Starter
Frenzied Member
Re: bring a richtextbox line to the top of the view
for my purposes, it's no matter. thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|