一起滚动两个富文本框,我想不通 [英] Scrolling two rich text boxes together, I can't figure it out

查看:26
本文介绍了一起滚动两个富文本框,我想不通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向上滚动 chatBox 时,我想向上滚动 timeBox.(不一定反之)

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

我找到了以下代码:

/// 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 个多小时试图让不同的代码工作,但我无法让它们中的任何一个工作,因为我只是相对刚刚开始编程,我不知道如何处理这段代码.

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.

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

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...

推荐答案

在测试您的代码后,它似乎工作正常.您的问题可能是您不知道如何使用代码,您必须将新的富文本框声明为 RichTextBoxSynchronizedScroll 而不是标准的 RichTextBox:

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() + "
";
     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

这篇关于一起滚动两个富文本框,我想不通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆