使两个ListBox一起滚动

时间:2015-11-13 20:57:13

标签: listbox scrollbar

我希望将两个ListBox一起滚动。

我有两个相同高度的ListBox,具有相同数量的项目等。我想设置它,如果用户在一个列表框中向上/向下滚动,则另一个ListBox的滚动条向上/向下滚动为好。

但我似乎找不到一种方法来检测滚动条位置值或检测它何时改变了值。

1 个答案:

答案 0 :(得分:0)

以下是同步两个ListBox的另一种方法:

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 (int i = 0; i < 40; i++)
            {
                listBox1.Items.Add("Item " + i);
                listBox2.Items.Add("Item " + i);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this._SyncListBoxes = new SyncListBoxes(this.listBox1, this.listBox2);
        }

    }

    public class SyncListBoxes
    {

        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;
                }
            }

        }
    }

}

enter image description here