PDA

Click to See Complete Forum and Search --> : RichTextBox Scrolling


bdakin
Jan 12th, 2000, 05:08 PM
I have two RichTextBox controls on a form and when I scroll one I want the other to scroll in exactly the same way. I can't find any scroll events for the RichTextBox so any other ideas would be greatly appreciated.

Thanks,

Brian.

Aaron Young
Jan 12th, 2000, 10:11 PM
Try changing the Value of the SelStart Property, this will reposition the Caret and Scroll the Text if neccessary, ie.

RichTextBox1.SelStart = Len(RichTextBox1) 'Scroll to the end of the Text

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

bdakin
Jan 13th, 2000, 12:12 AM
Brilliant!

That works great, thanks very much for all your help.

Brian.

bdakin
Jan 13th, 2000, 11:26 AM
Thanks for the reply.

I had thought of using the SelStart property but there were a couple of problems I couldn't figure out:

How do I know what to set it to, i.e. how do I know what the first character being displayed in the RTB is?

Also, I can't find a way of detecting when the scrollbars on a RTB are being used, the mouse click event only gets called when you click on the control itself.

Thanks,

Brian.

Aaron Young
Jan 13th, 2000, 11:45 AM
You could Subclass the 1st RTF Box to capture the WM_VSCROLL Message, thensend it to the 2nd RTF Box, ie.

In a Module..

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const GWL_WNDPROC = (-4)
Private Const WM_VSCROLL = &H115

Public lPrevWndProc As Long

Public Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_VSCROLL Then
'Scroll the 2nd RTFBox in Unison
Call SendMessage(Form1.RichTextBox2.hWnd, WM_VSCROLL, wParam, ByVal lParam)
End If
WindowProc = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, ByVal lParam)
End Function

In the Form with 2 RTF Boxes..

Private Sub Form_Load()
Dim iIndex As Integer

RichTextBox1 = ""
For iIndex = 1 To 100
RichTextBox1.SelText = iIndex & vbCrLf
Next
RichTextBox2 = RichTextBox1
lPrevWndProc = SetWindowLong(RichTextBox1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(RichTextBox1.hWnd, GWL_WNDPROC, lPrevWndProc)
End Sub


------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

bdakin
Jan 17th, 2000, 08:50 PM
Aaron,

I've been trying to extend the functionality of the code you posted so that when I scroll RTB1 it scrolls RTB2 and vice versa.

It works fine if only RTB1 controls RTB2 or if only RTB2 controls RTB1
but when I try to get them to control each other at the same time
VB just closes down (hope that made sense).

Any ideas?

Thanks,

Brian.

[This message has been edited by bdakin (edited 01-18-2000).]

[This message has been edited by bdakin (edited 01-18-2000).]

Aaron Young
Jan 17th, 2000, 09:57 PM
My first guess would be it's causing an Infinite loop, think about it..

You Scroll Box1 which sends a Message to Box2, which Scrolls Box2, which sends a Message to Box1, which Scrolls Box1, etc, etc..

You would need an Identifier so you would know when it's you who is programmatically scrolling either Box, then you could disregard the Scroll Message, i.e

Use a Private Boolean Variable in the Module, set this to True when you send a Scroll Message, then Check to see if this value is True, if it is, it means you shouldn't process the Scroll Message, then set it to False, ready to process the next Scroll Message, Preventing an Infinite loop.


------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

bdakin
Jan 19th, 2000, 08:08 PM
That did the trick, pretty obvious now that I think about it, thank you very much.

Brian.