La soluzione di Hans Passant è stata fantastica. Tuttavia avevo bisogno di sincronizzare tre caselle di testo non solo due.
Quindi l'ho modificato un po '- ma tutta l'affidabilità dovrebbe andare ad Hans, non c'è modo che mi sarei avvicinato senza il suo lavoro - ho pensato di riporlo qui nel caso in cui gli altri avessero bisogno dello stesso.
classe 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);
}
Poi nella forma initilizer:
// add the required controls into scroll sync
Control[] syncedCtrls = new Control[] { ctrl1, ctrl2, ..., ctrln };
foreach (SyncTextBox ctr in syncedCtrls)
{
ctr.Buddies = syncedCtrls;
}
Impossibile rispondere a meno che non ci dica quale tipo di framework GUI si sta utilizzando. – mikerobi
Solo default WinForms. – lesderid