Ramzes M
Ramzes M

Reputation: 33

Scrolling two rich text boxes together, I can't figure it out

I want to scroll timeBox up when I scroll chatBox up. (Not necessarily vice-versa)

I found the following code to do so:

/// Subclass RichTextBox to add the capability to bind scrolling for multiple RichTextBoxs.
/// This is useful for 'parallel' RTBs that require synchronized scrolling.
/// Taken from https://gist.github.com/593809
/// Added WM_HSCROLL 
/// Added BindScroll() to form a two-way linkage between RichTextBoxes.
/// Example usage showing how to bind 3 RichTextBoxes together:
/// rtb1.BindScroll(rtb2);
/// rtb2.BindScroll(rtb3);
/// rtb3.BindScroll(rtb1);

class RichTextBoxSynchronizedScroll : RichTextBox
{

    private const int WM_VSCROLL = 0x115;
    private const int WM_HSCROLL = 0x114;

    private List<RichTextBoxSynchronizedScroll> peers = new List<RichTextBoxSynchronizedScroll>();

    /// <summary>
    /// Establish a 2-way binding between RTBs for scrolling.
    /// </summary>
    /// <param name="arg">Another RTB</param>
    public void BindScroll( RichTextBoxSynchronizedScroll arg )
    {
        if ( peers.Contains( arg ) || arg==this ) { return; }
        peers.Add( arg );
        arg.BindScroll(this);
    }

    private void DirectWndProc(ref Message m)
    {
        base.WndProc(ref m);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL )
        {
            foreach (RichTextBoxSynchronizedScroll peer in this.peers)
            {
                Message peerMessage = Message.Create(peer.Handle, m.Msg, m.WParam, m.LParam);
                peer.DirectWndProc(ref peerMessage);
            }
        }

        base.WndProc(ref m);
    }
}

However, I've been busting my head over this for over 2 hours trying to get different codes to work, but I can't get any of them to work as I only relatively just started programming, and I can't figure out what to do with this code.

I've tried putting it as an extra class in my form's code, but then I couldn't actually apply BindScroll() to any textbox, as I couldn't reference to them or instance.

Maybe I could, but I don't know how. I've tried using just the code inside of the class, without it being a class by itself, but that caused errors.

Any help would be much appreciated...

Upvotes: 0

Views: 1658

Answers (2)

Matej Skulsky
Matej Skulsky

Reputation: 1

Ive been solving this whole 3hours... and I wanted something very simple. I considered every aspect of scrolling. You just need create VScrollBar and define scroll event like this

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        int position = text1.GetFirstCharIndexFromLine(vScrollBar1.Value);
        //position only 0.character on line
        textBox1.Select(position, 0);//if position=vScrollBar1.Value you would actually scrolling char by char
        textBox2.Select(position, 0);
        textBox1.ScrollToCaret();
        textBox2.ScrollToCaret();
    }

Hopefully I will be able to find this in future.

Upvotes: 0

King King
King King

Reputation: 63357

After testing your code, it seems to work OK. Your problem may be that you don't know how to use the code, you have to declare new richtextboxes as RichTextBoxSynchronizedScroll not as the standard RichTextBox:

//Here is the test
public partial class Form1 : Form {
   public Form1(){
     InitializeComponent();
     rb1.Size = new Size(200,100);
     rb2.Size = rb1.Size;
     rb2.Left = rb1.Right + 5;
     rb1.Parent = rb2.Parent = this;
     rtb1.BindScroll(rtb2);       
     //try populating some data for both the richtextboxes
     for(int i = 0; i < 200; i++)
        rtb1.Text += Guid.NewGuid() + "\r\n";
     rtb2.Text = rtb1;
     //now try scrolling the rtb1
     //I suggest you should add WM_MOUSEWHEEL = 0x20a into the if statement
     //something like this: if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL || m.Msg == WM_MOUSEWHEEL) ...
   }
   RichTextBoxSynchronizedScroll rtb1 = new RichTextBoxSynchronizedScroll();
   RichTextBoxSynchronizedScroll rtb2 = new RichTextBoxSynchronizedScroll();
}
//That's all

Upvotes: 2

Related Questions