Results 1 to 8 of 8

Thread: RichTextBox Scrolling

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    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.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    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
    [email protected]
    [email protected]


  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    Brilliant!

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

    Brian.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    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.

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You could Subclass the 1st RTF Box to capture the WM_VSCROLL Message, thensend it to the 2nd RTF Box, ie.

    In a Module..
    Code:
    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..
    Code:
    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
    [email protected]
    [email protected]


  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    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).]

  7. #7
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    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
    [email protected]
    [email protected]


  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    That did the trick, pretty obvious now that I think about it, thank you very much.

    Brian.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width