C#:同步两个RichTextBoxes滚动位置? [英] C#: Synchronize Scroll Position of two RichTextBoxes?
问题描述
在我的应用程序的形式,我有两个的RichTextBox
的对象。他们都将始终具有文本的行数相同。我想在这两者之间,以同步的垂直滚动,这样,当用户改变在一个垂直滚动位置,其他的滚动量相同。我怎么会去这样做?
In my application's form, I have two RichTextBox
objects. They will both always have the same number of lines of text. I would like to "synchronize" the vertical scrolling between these two, so that when the user changes the vertical scroll position on one, the other scrolls the same amount. How might I go about doing this?
推荐答案
我前一阵子这样做的一个小项目,和这里的simplist解决方案,我发现。
I did this for a small project a while ago, and here's the simplist solution I found.
创建一个新的控制由子类RichTextBox的:
Create a new control by subclassing RichTextBox:
public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTextBox
{
public event vScrollEventHandler vScroll;
public delegate void vScrollEventHandler(System.Windows.Forms.Message message);
public const int WM_VSCROLL = 0x115;
protected override void WndProc(ref System.Windows.Forms.Message msg) {
if (msg.Msg == WM_VSCROLL) {
if (vScroll != null) {
vScroll(msg);
}
}
base.WndProc(ref msg);
}
public void PubWndProc(ref System.Windows.Forms.Message msg) {
base.WndProc(ref msg);
}
}
添加新控件到窗体,并为每个控件明确通知其VSCROLL位置改变控制的其他实例。财产以后是这样的:
Add the new control to your form and for each control explicitly notify the other instances of the control that its vScroll position has changed. Somthing like this:
private void scrollSyncTxtBox1_vScroll(Message msg) {
msg.HWnd = scrollSyncTxtBox2.Handle;
scrollSyncTxtBox2.PubWndProc(ref msg);
}
我觉得这$ C $ c的问题,如果所有的关联性控件没有可显示的行数相同。
I think this code has problems if all the 'linked' controls don't have the same number of displayable lines.
这篇关于C#:同步两个RichTextBoxes滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!