滚动两种功能丰富的文本框在一起,我无法弄清楚 [英] Scrolling two rich text boxes together, I can't figure it out

查看:169
本文介绍了滚动两种功能丰富的文本框在一起,我无法弄清楚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要滚动时间盒,当我滚动客舱起来。 (不一定反之亦然)

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



不过,我一直在破坏我的头在这个超过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()的任何文本框,因为我couldn 'T对其引用或实例。

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 来声明新richtextboxes不是标准的的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() + "\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

这篇关于滚动两种功能丰富的文本框在一起,我无法弄清楚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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