当前位置: 首页 > 编程语言 > C#

在C#中同步多行文本框位置分享

时间:2023-04-10 18:06:44 C#

在C#中同步多行文本框位置我有一个C#应用程序,它有两个并排的多行文本框,每个文本框都在拆分容器的一侧。我想同步它们的垂直滚动,以便当用户向上或向下滚动其中一个文本框时,另一个分别向同一方向滚动。有没有办法做到这一点?谢谢。其他资源–7/26/10我在MSDN站点上发现了一些有趣的API:VisualC#2008ExpressEdition)抱怨,即使在添加PresenationFramework作为参考并插入使用System.Windows.Controls之后;在文件的顶部:错误1'System.Windows.Forms.TextBox'不包含'GetFirstVisibleLineIndex'的定义,并且找不到接受类型为'System.Windows.Forms.TextBox'的第一个参数的扩展方法'GetFirstVisibleLineIndex'(你错过了吗?使用指令或程序集引用?)其他东西–2010年7月27日我正在努力实施Jay关于实施新控件的建议,但我无法将事件处理程序绑定到控件中。这是我到目前为止为停止所拥有的:this.textBox1.Dispose();//替换为textBoxSync1this.textBox2.Dispose();//替换为textBoxSync2//绘制textBoxSync1this.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=newSystem.Drawing.Font("CourierNew",8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));this.textBoxSync1.Location=newSystem.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=newSystem.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);//绘制textBoxSync2this.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=newSystem.Drawing.Font("信使新",8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));this.textBoxSync2.Location=newSystem.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=newSystem.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);/*继续执行其他初始化...*/}privatevoidtextBoxSync1_VerticalScroll(Messagemsg){msg.HWnd=this.textBoxSync2.Handle;this.textBoxSync2.PubWndProc(refmsg);}privatevoidtextBoxSync2_VerticalScroll(Messagemsg){msg.HWnd=this.textBoxSync1.Handle;this.textBoxSync1.PubWndProc(refmsg);}}公共类TextBoxSynchronizedScroll:System.Windows.Forms.TextBox{公共事件vScrollEventHandlerVerticalScroll;publicdelegatevoidvScrollEventHandler(System.Windows.Forms.Messagemessage);publicconstintWM_VSCROLL=0x115;protectedoverridevoidWndProc(refSystem.Windows.Forms.Messagemsg){if(msg.Msg==WM_VSCROLL){if(VerticalScroll!=null){VerticalScroll(msg);}}}base.WndProc(refmsg);}publicvoidPubWndProc(refSystem.Windows.Forms.Messagemsg){base.WndProc(refmsg);我应该想...this.textBoxSync1.VerticalScroll+=newSystem.EventHandler(this.textBoxSync1_VerticalScroll);...需要将垂直滚动事件挂接到控件中,但正如您可能看到的那样,这不起作用,我们将不胜感激。谢谢。我会将RichTextBox子类化并侦听WM_VSCROLL消息以执行您正在尝试执行的操作。也许您可以这样做而不是使用TextBox。回复你的编辑:注意:我假设你在复制和粘贴到你的申请表时犯了一个小错误,那个textBoxSyncBusTraffic==textBoxSync1问题出在你控件的VerticalScroll事件声明中,在这一行:this.textBoxSyncBusTraffic.VerticalScroll+=newSystem.EventHandler(this.textBoxSyncBusTraffic_VerticalScroll);您的自定义控件需要订阅控件的TextBoxSynchronizedScroll.vScrollEventHandler事件(不是System.EventHandler)。事件处理程序中引用的方法不存在(至少在您发布的代码中不存在)。所以改变这个:this.textBoxSyncBusTraffic.VerticalScroll+=newSystem.EventHandler(this.textBoxSyncBusTraffic_VerticalScroll);为此:this.textBoxSync1.VerticalScroll+=newTextBoxSynchronizedScroll.vScrollEventHandler(textBoxSync1_VerticalScroll);并且您需要使用正确的事件处理程序和现有方法。另外,确保textBoxSync2的VerticalScroll事件声明如下:this.textBoxSync2.VerticalScroll+=newTextBoxSynchronizedScroll.vScrollEventHandler(textBoxSync2_VerticalScroll);顺便说一句,您可以使用几种技术更轻松地声明事件:第一种是使用窗体设计器。如果您在窗体设计器中的扩展控件实例的属性窗口中打开事件窗口,您将看到一个名为VerticalScroll的事件。双击它让VisualStudio声明事件并创建一个在事件触发时调用的方法。在代码中设置事件时也可以使用快捷方式。键入以下代码后,您会发现:youtextBoxSync1.VerticalScroll+=系统会提示您按Tab键来完成语句。如果这样做,VisualStudio将创建一个具有正确签名的方法。基于现有代码,我想出了以下内容。似乎对我有用。类TextBoxSynchronizedScroll:TextBox{publicconstintWM_VSCROLL=0x115;列出同行=newList();publicvoidAddPeer(TextBoxSynchronizedScrollpeer){this.peers.Add(peer);}privatevoidDirectWndProc(refMessagem){base.WndProc(refm);}protectedoverridevoidWndProc(refMessagem){if(m.Msg==WM_VSCROLL){foreach(varpeerinthis.peers){varpeerMessage=Message.Create(peer.Handle,m.Msg,m.WParam,m.LParam);peer.DirectWndProc(refpeerMessage);}}base.WndProc(refm);}}http://gist.github.com/593809以上是C#学习教程:在C#同步多行文本框位置分享的所有内容,如果对你有用还需要详细了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右方联系管理。会员删除。如需转载请注明出处: