Results 1 to 8 of 8

Thread: Allow RichTextBox Scrollbar to control another RTB scroll

  1. #1

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Allow RichTextBox Scrollbar to control another RTB scroll

    I want to be able to have a richtextbox scroll event synced with the scroll of another richtextbox. Do any of you know how this is possible?

  2. #2

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    I'm guessing this is something that not many people know how to do? Looks like I might have a bit of a complicated code for this one.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    try this. i got the scrollbar scrolling working well, but the mousewheel scrolling is laggy:
    Attached Files Attached Files

  4. #4

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    I'll try to learn from it. I might come back to this thread to ask you a few things about the code as well, if you have the time to help me understand anything I don't know.

    Thanks though I appreciate that.

    After Testing: It does appear that the mouse scroll is laggy by a bit. It's only behind by one tick on my scroller though. Somehow it's getting delayed. I also noticed that it's not quite realtime with the side scroll when manually scrolling with the bar. It adusts after the mouse up event, and the scroller doesn't change with the up or down arrows when the screen goes up or down within the content area. This is going to be quite a trick, I always wondered how of what they did with a program like Notepad++ to get everything in sync as though they were only one control... This is an advancement to what I had before though.
    Last edited by AceInfinity; Jun 22nd, 2011 at 10:28 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    Quote Originally Posted by AceInfinity View Post
    It does appear that the mouse scroll is laggy by a bit. It's only behind by one tick on my scroller though. Somehow it's getting delayed.
    Not sure exactly how to fix that but I noticed if you change the RTB smooth scrolling to single line scrolls instead then the mousewheel works much better.

    Code:
    ' add to top of rtbEx.vb,
    Private Const EM_SCROLL As Int32 = &HB5
    Private Const SB_LINEDOWN As Int32 = 1
    Private Const SB_LINEUP As Int32 = 0
    
    ' change rtbEx.vb,
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        ' change mousewheel msgs to scroll one line instead of smooth scrolls.
        If m.Msg = WM_MOUSEWHEEL Then
            m.Msg = EM_SCROLL
            m.LParam = IntPtr.Zero
            If (m.WParam.ToInt32) / 65536 > 0 Then ' wheel direction upward
                m.WParam = CType(SB_LINEUP, IntPtr)
            Else ' wheel direction downward
                m.WParam = CType(SB_LINEDOWN, IntPtr)
            End If
        End If
        
        MyBase.WndProc(m)
        ' detect EM_SCROLL here 
        If m.Msg = WM_VSCROLL OrElse m.Msg = WM_HSCROLL OrElse m.Msg = EM_SCROLL Then
            SendScrollPosMessage(scrollMatchedControl.Handle, EM_SETSCROLLPOS, New IntPtr(0), scrollHandler.getScrollValues(Me.Handle))
        End If
    
    End Sub

  6. #6

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    Hmm, you have a point, and I could see how that would be much more beneficial for my purposes as well, it would keep the mouse scrolling more in sync with the lines in my control. I'll test it out, and based on that idea, I could probably think of a way to add another extra bonus function for changing the number of lines to scroll from each mouse scroll "tick" just like in the general mouse options for Windows.

    I did manage to get a little better control of the synced scrolling, but I'm not sure on how I could get them to scroll together by using the up and down arrows. If I figure that out I'll post here with my finalized code to help others out too.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    Quote Originally Posted by AceInfinity View Post
    I did manage to get a little better control of the synced scrolling, but I'm not sure on how I could get them to scroll together by using the up and down arrows. If I figure that out I'll post here with my finalized code to help others out too.
    Just curious, have you tried the clsRTFScrollSync class? I tried it in WinXP-32 only with VB10 and the scrollthumbs, keyboard arrows/page Up/Down and even the middle mouse arrow thingy kept both RTBs scrollbars in sync.

    One problem I ran into in VB10 with Option Strict On was that VB complains of late binding in the Public Sub AddControl, tho haven't bothered to figure out how to fix that as I really have no use for this.

    I setup two RTBs the same and tried it like this,
    Code:
    Private objScrollSync As New clsRTFScrollSync
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objScrollSync.ScrollBarToSync = ScrollBars.Both
        objScrollSync.AddControl(RichTextBox1)
        objScrollSync.AddControl(RichTextBox2)
    End Sub

  8. #8

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Allow RichTextBox Scrollbar to control another RTB scroll

    I'm using a trick method that seems to kind of work, suggested by another member here, I think paul already gave me that method, and it did work, but it was a bit laggy
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Tags for this Thread

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