Search code examples
c#.netcustom-controlsscrollbar

same scroll bar for two richtextboxes


Is there any third party tool available which has two richtextboxes but only a shared scroll bar for both. I need to implement some text in two different languages but both the textboxes should scroll at the same time.


Solution

  •     public enum ScrollBarType : uint
        {
            SbHorz = 0,
            SbVert = 1,
            SbCtl = 2,
            SbBoth = 3
        }
    
        public enum Message : uint
        {
            WM_VSCROLL = 0x0115
        }
    
        public enum ScrollBarCommands : uint
        {
            SB_THUMBPOSITION = 4
        }
    
        [DllImport("User32.dll")]
        public extern static int GetScrollPos(IntPtr hWnd, int nBar);
    
        [DllImport("User32.dll")]
        public extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
       // Set the dual scrolling on the richTextbox1 and affects richTextbox2
    
        private void richTextBox1_VScroll(object sender, EventArgs e)
        {
            int nPos = GetScrollPos(richTextBox1.Handle, (int)ScrollBarType.SbVert); 
            nPos <<= 16;
            uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
            SendMessage(richTextBox2.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0));
        }