0
votes

I wish to make two ListBoxes scroll together.

I have two ListBoxes of the same height with the same number of items, etc. I want to set it up such that if the user scrolls up/down in one list box the scrollbar for the other ListBox scrolls up/down as well.

But I can not seem to find a way to either detect the scroll bar position value or to detect when it has changed value.

1
I figured out how to change the vertical scroll with the TopIndex property. Seting that changing scroll bar pos. BUT I cannot detect when the user moves the SB. In other words I can change the SB value, but there is no event correlated with "TopIndex". I thought that I could trigger with .draw since the image has to be redrawn when scrolled, but I guess draw doesn't do that. I am very new to C#. If there is some way to set an event up to trigger with TopIndex or, better yet, some existing event that triggers every time something visually changes in a listbox, that would be great. - Popinjay
I found this, but I do not know how to activate the event once I made the class. Again, I am new to C# social.msdn.microsoft.com/Forums/en-US/… - Popinjay

1 Answers

0
votes

Here is another way to sync the two ListBoxes:

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