额外信息 - 7/26/10
我在MSDN网站上找到了一些有趣的API:
TextBox.GetFirstVisibleLineIndex方法 TextBox.GetLastVisibleLineIndex方法 TextBox.ScrollToLine方法
那里的文档看起来很有前途,但是我的编译器(Microsoft Visual C# 2008 Express Edition)在我尝试使用它时发出警告,即使我已经将PresenationFramework
作为引用添加并在文件顶部插入了using System.Windows.Controls;
:
Error 1 'System.Windows.Forms.TextBox' does not contain a definition for 'GetFirstVisibleLineIndex' and no extension method 'GetFirstVisibleLineIndex' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)
附加信息 - 7/27/10
我正在实现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);
需要将垂直滚动事件与控件连接起来,但正如您所看到的,这并不起作用。任何建议都将不胜感激。谢谢。