La solución de Hans Passant fue increíble. Sin embargo, necesitaba sincronizar tres cuadros de texto, no solo dos.
Así que lo modifiqué un poco, pero toda la credibilidad debería ir a Hans, no hay forma de que incluso me hubiera acercado sin su trabajo, pensé que lo publicaría de nuevo aquí en caso de que otros necesiten lo mismo.
clase SyncBox:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class SyncTextBox : TextBox
{
public SyncTextBox()
{
this.Multiline = true;
this.ScrollBars = ScrollBars.Vertical;
}
public Control[] Buddies { get; set; }
private static bool scrolling; // In case buddy tries to scroll us
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// Trap WM_VSCROLL message and pass to buddy
if (Buddies != null)
{
foreach (Control ctr in Buddies)
{
if (ctr != this)
{
if ((m.Msg == 0x115 || m.Msg == 0x20a) && !scrolling && ctr.IsHandleCreated)
{
scrolling = true;
SendMessage(ctr.Handle, m.Msg, m.WParam, m.LParam);
scrolling = false;
}
}
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
Luego, en la forma initilizer:
// add the required controls into scroll sync
Control[] syncedCtrls = new Control[] { ctrl1, ctrl2, ..., ctrln };
foreach (SyncTextBox ctr in syncedCtrls)
{
ctr.Buddies = syncedCtrls;
}
Imposible responder a menos que nos diga qué tipo de marco de GUI está utilizando. – mikerobi
Simplemente por defecto WinForms. – lesderid