将两个富文本框滚动在一起,我无法弄明白
我想滚动聊天框时向上滚动时间框。 (不一定反之亦然)
我找到了以下代码:
/// 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 peers = new List(); /// /// Establish a 2-way binding between RTBs for scrolling. /// /// Another RTB 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()应用于任何文本框,因为我无法引用它们或实例。
也许我可以,但我不知道如何。 我尝试只使用类中的代码,而不是它本身就是一个类,但这会导致错误。
任何帮助将非常感激…
在测试代码后,它似乎工作正常。 您的问题可能是您不知道如何使用代码,您必须将新的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
我整整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(); }
希望将来我能找到这个。
- c#.net 4.5 async / multithread?
- 如何以编程方式检测Windows Phone 8.1操作系统版本?
- 具有动态maxCount的SemaphoreSlim
- Howto:Parallel.Foreach在每个进程运行一个新进程(但一次只有一个进程)后执行许多进程?
- 安装文件的位置 – Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.5
- 在Windows Server 2012中启动更改
- System.Web.Helpers未由Visual Studio 2012发布
- .NET 4.5中的序列化中断
- HttpClient:如何一次上传多个文件