将两个富文本框一起滚动,我无法弄明白

时间:2013-07-30 01:57:15

标签: c# winforms class instance .net-4.5

我想滚动聊天框时向上滚动时间框。 (不一定反之亦然)

我找到了以下代码:

/// 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);
    }
}

然而,我一直在试图让不同的代码工作超过2个小时,但我无法让它们中的任何一个工作,因为我只是只是开始编程,我无法弄清楚如何处理这段代码。

我已经尝试将它作为一个额外的类放在我的表单代码中,但是我实际上无法将BindScroll()应用于任何文本框,因为我无法引用它们或实例。

也许我可以,但我不知道如何。我已经尝试过只使用类中的代码,而不是它本身就是一个类,但这会导致错误。

非常感谢任何帮助......

2 个答案:

答案 0 :(得分:1)

在测试代码后,它似乎工作正常。您的问题可能是您不知道如何使用代码,您必须将新的richtextbox声明为RichTextBoxSynchronizedScroll而不是标准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

答案 1 :(得分:0)

我已经解决了整整3个小时......我想要一些非常简单的东西。我考虑了滚动的各个方面。 您只需要创建VScrollBar 并定义像这样的滚动事件

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();
    }

希望将来我能找到这个。