using System.Windows.Forms; public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTextBox { public SynchronizedScrollRichTextBox Synchronized { get; set; } public const int WM_VSCROLL = 0x115; public const int EM_LINESCROLL = 0xB6; protected override void WndProc(ref System.Windows.Forms.Message msg) { if (msg.Msg == WM_VSCROLL || msg.Msg == EM_LINESCROLL) { if (Synchronized != null) { Message message = msg; message.HWnd = Synchronized.Handle; Synchronized.PubWndProc(ref message); } } base.WndProc(ref msg); } public void PubWndProc(ref System.Windows.Forms.Message msg) { base.WndProc(ref msg); } }
思路参考了中文英文多篇资料,没有比较完整的,所以,算原创也不为过。
我的博客向来简单明了,以上代码,复制到你项目中。编译一次,拖到窗体中即可,例如:
拖2个实例,然后在设计界面,分别设置 Synchronized 属性为对方。运行,两个文本框就可以关联起来了。