3

I am using 2 Rich Text Boxes on winforms 4 (customRTB1 and customRTB2). Both of the rtb's have same text. What I want to achieve is, when one rtb (customRTB1) is scrolled down, the other rtb (customRTB2) also should be scrolled to exactly same position as customRTB1. I attempted this:

public class CustomRTB : RichTextBox
    {
        #region API Stuff
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetScrollPos(IntPtr hWnd, int nBar);

        [DllImport("user32.dll")]
        public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

        private const int SB_HORZ = 0x0;
        private const int SB_VERT = 0x1;
        #endregion
        public int HorizontalPosition
        {
            get { return GetScrollPos((IntPtr)this.Handle, SB_HORZ); }
            set { SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); }
        }

        public int VerticalPosition
        {
            get { return GetScrollPos((IntPtr)this.Handle, SB_VERT); }
            set { SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); }
        }
    }

Using HorizontalPosition and VerticalPosition I could move the scroll bar of SECOND rtb as follows:

private void customRTB1_VScroll(object sender, EventArgs e)
{
          customRTB2.VerticalPosition = customRTB1.VerticalPosition;
}

This moves the scroll bar of second rtb to the position of first rtb, however, it DOES NOT move the text at all! So how to make this second rtb to show the corresponding text according to the scroll bar's position? Mainly, I want every activity happening (editing, scrolling etc)of first rtb to repeat on second rtb. I know I am very close to the solution. Please help.

Aditya Bokade
  • 1,708
  • 1
  • 28
  • 45
  • 2
    I think [this][1] may solve your problem. [1]: http://stackoverflow.com/questions/1827323/c-synchronize-scroll-position-of-two-richtextboxes – Tomtom Aug 15 '12 at 08:25
  • Thanks Tomtom..you solved my problem. I wanted to create my own presentation (PPT kind of) application. One form has one rtb1 that is displayed to me on my laptop with my own notes. Other form is displayed on EXTENDED (dual) monitor to audience it just shows rtb2. But I wanted second rtb to display exactly SAME stuff that of rtb1, that I can see. You made my life easy, made my day. God bless you! – Aditya Bokade Aug 15 '12 at 10:04
  • There is one more small problem. If I use Scroll bar of rtb1, it also moves the scroll bar of rtb2. But If I click "page down" button in rtb1, then it does't scroll rtb2. This was very important. What can I do? When I click page down button and then click the scroll bar of rtb1, it reflects in rtb2. What to do? – Aditya Bokade Aug 15 '12 at 10:21

0 Answers0