2015-11-13 44 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