【发布时间】:2010-09-23 23:36:49
【问题描述】:
我有一个 C# 应用程序,其中有两个并排的多行文本框,每个文本框位于拆分容器的一侧。我想同步他们的垂直滚动,以便当用户向上或向下滚动其中一个文本框时,另一个文本框分别沿相同方向滚动。有没有办法做到这一点?谢谢。
附加信息 - 2010 年 7 月 26 日
我在 MSDN 网站上发现了一些有趣的 API:
TextBox.GetFirstVisibleLineIndex Method
TextBox.GetLastVisibleLineIndex Method
TextBox.ScrollToLine Method
那里的文档看起来很有希望,但是当我尝试使用它时,我的编译器(Microsoft Visual C# 2008 Express Edition)会报错,即使在添加 PresenationFramework 作为参考并在文件顶部插入 using System.Windows.Controls; 之后:
错误 1“System.Windows.Forms.TextBox”不包含“GetFirstVisibleLineIndex”的定义,并且找不到接受“System.Windows.Forms.TextBox”类型的第一个参数的扩展方法“GetFirstVisibleLineIndex”(是您缺少 using 指令或程序集引用?)
附加信息 - 2010 年 7 月 27 日
我正在努力实施 Jay 关于实施新控件的建议,但我无法将事件处理程序绑定到控件中。这是我目前所拥有的:
public partial class MyFormApplication : Form
{
public MyFormApplication() // MyFormApplication constructor
{
this.InitializeComponent();
this.textBox1.Dispose(); // Replacing with textBoxSync1
this.textBox2.Dispose(); // Replacing with textBoxSync2
// Draw textBoxSync1
this.textBoxSync1.AcceptsReturn = true;
this.textBoxSync1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxSync1.BackColor = System.Drawing.SystemColors.Control;
this.textBoxSync1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxSync1.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxSync1.Location = new System.Drawing.Point(0, 19);
this.textBoxSync1.Multiline = true;
this.textBoxSync1.Name = "textBoxSync1";
this.textBoxSync1.ReadOnly = true;
this.textBoxSync1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBoxSync1.Size = new System.Drawing.Size(338, 231);
this.textBoxSync1.TabIndex = 0;
this.textBoxSync1.TabStop = false;
this.textBoxSync1.WordWrap = false;
this.splitContainer1.Panel1.Controls.Remove(this.textBox1);
this.splitContainer1.Panel1.Controls.Add(this.textBoxSync1);
// Draw textBoxSync2
this.textBoxSync2.AcceptsReturn = true;
this.textBoxSync2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxSync2.BackColor = System.Drawing.SystemColors.Control;
this.textBoxSync2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxSync2.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxSync2.Location = new System.Drawing.Point(0, 19);
this.textBoxSync2.Multiline = true;
this.textBoxSync2.Name = "textBoxSync2";
this.textBoxSync2.ReadOnly = true;
this.textBoxSync2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBoxSync2.Size = new System.Drawing.Size(113, 231);
this.textBoxSync2.TabIndex = 30;
this.textBoxSync2.TabStop = false;
this.textBoxSync2.WordWrap = false;
this.splitContainer1.Panel2.Controls.Remove(this.textBox2);
this.splitContainer1.Panel2.Controls.Add(this.textBoxSync2);
/* Goes on to perform other initializations... */
}
private void textBoxSync1_VerticalScroll(Message msg)
{
msg.HWnd = this.textBoxSync2.Handle;
this.textBoxSync2.PubWndProc(ref msg);
}
private void textBoxSync2_VerticalScroll(Message msg)
{
msg.HWnd = this.textBoxSync1.Handle;
this.textBoxSync1.PubWndProc(ref msg);
}
}
public class TextBoxSynchronizedScroll : System.Windows.Forms.TextBox
{
public event vScrollEventHandler VerticalScroll;
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 (VerticalScroll != null)
{
VerticalScroll(msg);
}
}
base.WndProc(ref msg);
}
public void PubWndProc(ref System.Windows.Forms.Message msg)
{
base.WndProc(ref msg);
}
}
我应该认为类似...
this.textBoxSync1.VerticalScroll += new System.EventHandler(this.textBoxSync1_VerticalScroll);
...需要将垂直滚动事件挂钩到控件中,但正如您可能看到的那样,这不起作用。任何建议,将不胜感激。谢谢。
【问题讨论】:
标签: c# synchronization scroll textbox