1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| public class Sync { private ListBox _LB1 = null; private ListBox _LB2 = null;
private ListBoxScroll _ListBoxScroll1 = null; private ListBoxScroll _ListBoxScroll2 = null;
public Sync(ListBox LB1, ListBox LB2) { if (LB1 != null && LB1.IsHandleCreated && LB2 != null && LB2.IsHandleCreated && LB1.Items.Count == LB2.Items.Count && LB1.Height == LB2.Height) { this._LB1 = LB1; this._ListBoxScroll1 = new ListBoxScroll(LB1); this._ListBoxScroll1.Scroll += _ListBoxScroll1_VerticalScroll;
this._LB2 = LB2; this._ListBoxScroll2 = new ListBoxScroll(LB2); this._ListBoxScroll2.Scroll += _ListBoxScroll2_VerticalScroll;
this._LB1.SelectedIndexChanged += _LB1_SelectedIndexChanged; this._LB2.SelectedIndexChanged += _LB2_SelectedIndexChanged; } }
private void _LB1_SelectedIndexChanged(object sender, EventArgs e) { if (this._LB2.TopIndex != this._LB1.TopIndex) { this._LB2.TopIndex = this._LB1.TopIndex; } }
private void _LB2_SelectedIndexChanged(object sender, EventArgs e) { if (this._LB1.TopIndex != this._LB2.TopIndex) { this._LB1.TopIndex = this._LB2.TopIndex; } }
private void _ListBoxScroll1_VerticalScroll(ListBox LB) { if (this._LB2.TopIndex != this._LB1.TopIndex) { this._LB2.TopIndex = this._LB1.TopIndex; } }
private void _ListBoxScroll2_VerticalScroll(ListBox LB) { if (this._LB1.TopIndex != this._LB2.TopIndex) { this._LB1.TopIndex = this._LB2.TopIndex; } }
private class ListBoxScroll : NativeWindow { private ListBox _LB = null; private const int WM_VSCROLL = 0x115; private const int WM_MOUSEWHEEL = 0x20a;
public event dlgListBoxScroll Scroll; public delegate void dlgListBoxScroll(ListBox LB);
public ListBoxScroll(ListBox LB) { this._LB = LB; this.AssignHandle(LB.Handle); }
protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case WM_VSCROLL: case WM_MOUSEWHEEL: if (this.Scroll != null) { this.Scroll(_LB); } break; } } } }
|