C# 同步兩個ListBox滾動條

有時需要對2個listbox的選項一一對比,兩邊來回拖容易搞亂搞錯,所以google了一下,找到了幾個listbox同步的方法。
同時也親自試驗了一下,先把成功的貼上來

1 、方法一

Winform介面裡放入2個listbox——listBox1,listBox2,From1新增程式碼:

       public Form1()
{
InitializeComponent();
//新增選項
<span style="white-space:pre">	</span>   for (int i = 0; i <40; i  )
{
listBox1.Items.Add("Items "   i);
listBox2.Items.Add("Items "   i);
}
//新增同步
EventHandler handler = (s, e) =>
{
if(s == listBox1)
listBox2.TopIndex =listBox1.TopIndex;
if(s == listBox2)
listBox1.TopIndex =listBox2.TopIndex;
};
this.listBox1.MouseCaptureChanged  =handler;
this.listBox2.MouseCaptureChanged  =handler;
this.listBox1.SelectedIndexChanged  =handler;
this.listBox2.SelectedIndexChanged  =handler;
}

執行後顯示:

 優點:程式碼量少,簡單

 缺點:不支援滑鼠滾輪,兩邊都是獨立選中,不會自動選中對齊

 

2 、方法二

在form1中新增2個listBox——listBox1,listBox2,進入程式碼介面:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SyncTwoListBox
{
public partial class Form1 : Form
{
private SyncListBoxes_SyncListBoxes = null;
public Form1()
{
InitializeComponent();
this.Load  = Form1_Load;
//add options
for (inti = 0; i < 40; i  )
{
listBox1.Items.Add("Item "   i);
listBox2.Items.Add("Item "   i);
}
}
private voidForm1_Load(object sender, EventArgs e)
{
this._SyncListBoxes = new SyncListBoxes(this.listBox1, this.listBox2);
}
}
//建立一個listbox同步的類
public class SyncListBoxes
{
<span style="white-space:pre">	</span>    private ListBox _LB1 = null;
        private ListBox _LB2 = null;
        private ListBoxScroll _ListBoxScroll1 = null;
        private ListBoxScroll _ListBoxScroll2 = null;
        public SyncListBoxes(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;
            }
            if (this._LB2.SelectedIndex != this._LB1.SelectedIndex)
            {
                this._LB2.SelectedIndex = this._LB1.SelectedIndex;
            }
        }
        private void _LB2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this._LB1.TopIndex != this._LB2.TopIndex)
            {
                this._LB1.TopIndex = this._LB2.TopIndex;
            }
            if (this._LB1.SelectedIndex != this._LB2.SelectedIndex)
            {
                this._LB1.SelectedIndex = this._LB2.SelectedIndex;
            }
        }
        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;
                }
            }
        }
}
}

執行後顯示:

優點:支援滾輪、支援同步選中,直接使用listbox

缺點:滾輪同步略有延遲

3、方法三

還有其他方法,我還在試驗中,成功後後期補充上來。