2015-11-13 45 views
0

我想提出两个ListBox一起滚动。让两个ListBox滚动起来

我有两个相同的高度与相同数量的项目等列表框我想要设置它,如果用户在一个列表框中滚动向上/向下滚动其他ListBox的滚动条向上/向下滚动以及。

但我似乎无法找到一种方法来检测或者滚动条的位置值,或者当它改变值来检测。

+0

我想出如何与TopIndex属性更改垂直滚动。设置更改滚动条的位置。但我无法检测到用户何时移动SB。 换句话说我可以改变SB值,但不存在与“TopIndex”相关联的事件。我认为我可以用.draw触发,因为图像在滚动时必须重画,但我想绘画不会那样做。 我很新的C#。如果有一些方法来设置一个事件了TopIndex触发或者更好的是,现有的一些事件触发每次的东西在ListBox视觉上的变化,这将是巨大的。 – Popinjay

+0

我发现了这个,但我不知道如何激活事件,一旦我上了课。我是新来的C# https://social.msdn.microsoft.com/Forums/zh-CN/46d8cba4-1266-4f39-a27b-5e86a4cf3583/listbox-verticle-scroll-bar-event?forum = Vsexpressvcs – Popinjay

回答

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