本文主要是介绍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 optionsfor (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、方法三
还有其他方法,我还在试验中,成功后后期补充上来。
这篇关于C# 同步两个ListBox滚动条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!