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、方法三

还有其他方法,我还在试验中,成功后后期补充上来。




  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值