Tuesday, May 17, 2011

Synchronized RichTextBox objects (or how to scroll 2 RichTextBox objects together)

In my previous posts I have described, how to create synchronized (or linked) ListBox and ListView objects. Now I continue in investigation in this area and I have found another interesting example which describes synchronization of two RichTextBox objects. First step is to create class which derives from RichTextBox class.
public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTextBox
{
    public event vScrollEventHandler vScroll;
    public delegate void vScrollEventHandler(System.Windows.Forms.Message message);

    public const int WM_VSCROLL = 0x115;

    protected override void WndProc(ref System.Windows.Forms.Message msg)
    {
        if (msg.Msg == WM_VSCROLL)
        {
            if (vScroll != null)
            {
                vScroll(msg);
            }
        }
        base.WndProc(ref msg);
    }

    public void PubWndProc(ref System.Windows.Forms.Message msg)
    {
        base.WndProc(ref msg);
    }
}
I have created two SynchronizedScrollRichTextBox controls and placed them on the form. Then I have added following code (vScroll event):
private void synchronizedScrollRichTextBox1_vScroll(Message message)
{
    message.HWnd = synchronizedScrollRichTextBox2.Handle;
    synchronizedScrollRichTextBox2.PubWndProc(ref message);
}
Here you can find original post.

No comments:

Post a Comment