synchronization - Synchronizing Multiline Textbox Positions in C# -


i have c# application wherein there 2 multiline textboxes side-by-side, each in 1 side of split-container. synchronize vertical scroll, when user scrolls or down 1 of textboxes, other textbox scrolls respectively in same direction. there way this? thanks.

additional information - 7/26/10

i found interesting apis on msdn website:

textbox.getfirstvisiblelineindex method
textbox.getlastvisiblelineindex method
textbox.scrolltoline method

the documentation there looks promising, compiler (microsoft visual c# 2008 express edition) complains when try use it, after adding presenationframework reference , inserting using system.windows.controls; @ top of file:

error 1 'system.windows.forms.textbox' not contain definition 'getfirstvisiblelineindex' , no extension method 'getfirstvisiblelineindex' accepting first argument of type 'system.windows.forms.textbox' found (are missing using directive or assembly reference?)

additional information - 7/27/10

i'm working on implementing jay's suggestion of implementing new control, i'm having trouble tying eventhandler control. here's have far:

public partial class myformapplication : form {   public myformapplication() // myformapplication constructor   {      this.initializecomponent();       this.textbox1.dispose(); // replacing textboxsync1      this.textbox2.dispose(); // replacing 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 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);   } } 

i should think like...

this.textboxsync1.verticalscroll += new system.eventhandler(this.textboxsync1_verticalscroll); 

...would needed hook vertical scroll event control, can see, not work. suggestions appreciated. thanks.

i subclassed richtextbox , listened wm_vscroll message you're trying do. perhaps can instead of using textbox.

response edit:

note: i'm assuming made minor error in copy , paste in application form , textboxsyncbustraffic == textboxsync1

the problem in declaration of control's verticalscroll event, in line:

this.textboxsyncbustraffic.verticalscroll += new system.eventhandler(this.textboxsyncbustraffic_verticalscroll);  
  1. your custom controls need subscribe controls' textboxsynchronizedscroll.vscrolleventhandler events (not system.eventhandler).
  2. the method referenced in event handler doesn't exist (at least not in code posted).

so change this:

this.textboxsyncbustraffic.verticalscroll += new system.eventhandler(this.textboxsyncbustraffic_verticalscroll);  

to this:

this.textboxsync1.verticalscroll += new textboxsynchronizedscroll.vscrolleventhandler(textboxsync1_verticalscroll); 

this uses correct event handler , references method need , have.

also, make sure declaration textboxsync2's verticalscroll event looks this:

this.textboxsync2.verticalscroll += new textboxsynchronizedscroll.vscrolleventhandler(textboxsync2_verticalscroll); 

incidentally, there couple techniques can use make easier declare events:

the first use form designer. if open events window in properties window of instance of extended control in forms designer, you'll see event called verticalscroll. double click item have visual studio declare event , create method call when event fires.

there's shortcut can use when set event in code. you'll find after type following code:

youtextboxsync1.verticalscroll += 

you'll prompted press tab finish declaration. if visual studio create method correct signature.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -