Synchronize RichTextBox Vertical Scrolling with SplitContainer panel scrolling

1.2k views Asked by At

I have a win form with a SplitContainer

The SplitContainer's panel1 consists of a RichTextBox.

Panel2 AutoScroll is set to true.

I want to synchronize the scrolling of RichTextBox and Panel2 vice versa. How can I do that? any idea?

I've tried this and it is working for two RichTextBoxes but not in my case.

1

There are 1 answers

1
devHead On

Get the scroll info for both controls:

First you will need the following win32 API's (Imports System.Runtime.InteropServices) into your project

<DllImport("user32.dll")> _
Private Shared Function GetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, ByRef lpsi As SCROLLINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll")> _
Private Shared Function SetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, <[In]()> ByRef lpsi As SCROLLINFO, ByVal fRedraw As Boolean) As Integer
End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function

'added by edit
Private Const SB_THUMBTRACK As Integer = 5
Private Const WM_VSCROLL As Integer = &H115
Private Const WM_HSCROLL As Integer = &H114

Public Declare Function SetScrollPos Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal nBar As Integer, _
    ByVal nPos As Integer, _
    ByVal bRedraw As Boolean) As Integer

    Private Structure SCROLLINFO
        Public cbSize As UInteger
        Public fMask As UInteger
        Public nMin As Integer
        Public nMax As Integer
        Public nPage As UInteger
        Public nPos As Integer
        Public nTrackPos As Integer
    End Structure

    Private Enum ScrollBarDirection
        SB_HORZ = 0
        SB_VERT = 1
        SB_CTL = 2
        SB_BOTH = 3
    End Enum

    Private Enum ScrollInfoMask
        SIF_RANGE = &H1
        SIF_PAGE = &H2
        SIF_POS = &H4
        SIF_DISABLENOSCROLL = &H8
        SIF_TRACKPOS = &H10
        SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
    End Enum

 'create some public properties to get and set scroll position for any scrollable control:

 Public Property _VerticalScroll(ByVal hwnd As IntPtr) As Integer
     Get
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         Return si.nPos
     End Get
     Set(ByVal value As Integer)
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         If value > si.nMax Then
             value = si.nMax
         End If

         Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
         SetScrollPos(hwnd, Orientation.Vertical, value, True)
         PostMessage(hwnd, WM_VSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

Public Property _HorizontalScroll(ByVal hwnd As IntPtr) As Integer
    Get
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        Return si.nPos
    End Get
    Set(ByVal value As Integer)
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        If value > si.nMax Then
            value = si.nMax
        End If

        Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
        SetScrollPos(hwnd, Orientation.Horizontal, value, True)
        PostMessage(hwnd, WM_HSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

your next move is to monitor which scroll bar changes....via timer etc... then update the other control when needed.