vitek2424 Asked:2020-12-16 23:51:27 +0000 UTC2020-12-16 23:51:27 +0000 UTC 2020-12-16 23:51:27 +0000 UTC 多个列表框winforms c#的同步滚动 772 winforms中有4个listbox,item的个数总是一样的,如何实现键盘(箭头)、鼠标滚轮和滑块的同步滚动。滑块应仅留在其中一个列表框上。提前致谢! c# 1 个回答 Voted Best Answer NasIta Highborne 2020-12-17T00:47:58Z2020-12-17T00:47:58Z 应该在发生滚动的 ListBox 上订阅滚动事件,并在该事件的处理程序中,设置其余列表的TopIndex值(该值应与 main 相同)一)。 在我们的例子中,来自 WinForms 的 ListBox 没有自己的滚动事件,所以我们将改进我们的列表: using System; using System.Windows.Forms; public class BetterListBox : ListBox { // Event declaration public delegate void BetterListBoxScrollDelegate(object Sender, BetterListBoxScrollArgs e); public event BetterListBoxScrollDelegate Scroll; // WM_VSCROLL message constants private const int WM_VSCROLL = 0x0115; private const int SB_THUMBTRACK = 5; private const int SB_ENDSCROLL = 8; protected override void WndProc(ref Message m) { // Trap the WM_VSCROLL message to generate the Scroll event base.WndProc(ref m); if (m.Msg == WM_VSCROLL) { int nfy = m.WParam.ToInt32() & 0xFFFF; if (Scroll != null && (nfy == SB_THUMBTRACK || nfy == SB_ENDSCROLL)) Scroll(this, new BetterListBoxScrollArgs(this.TopIndex, nfy == SB_THUMBTRACK)); } } public class BetterListBoxScrollArgs { // Scroll event argument private int mTop; private bool mTracking; public BetterListBoxScrollArgs(int top, bool tracking) { mTop = top; mTracking = tracking; } public int Top { get { return mTop; } } public bool Tracking { get { return mTracking; } } } 显然,现在我们只通过代码添加改进的 ListBox 会更容易。 这很容易完成: var list1 = new BetterListBox(); list1.Width = 200; list1.Height = 200; Controls.Add(list1); 现在让我们订阅该事件: list1.Scroll += BetterListBox1_Scroll; BetterListBox1_Scroll我们的事件处理程序在哪里: void BetterListBox1_Scroll(object s, BetterListBoxScrollArgs e) { // здесь будут наши обновления других списков } 让我们更新其余的列表: void BetterListBox1_Scroll(object s, BetterListBoxScrollArgs e) { list4.TopIndex = list3.TopIndex = list2.TopIndex = list1.TopIndex; } 准备好!我亲自检查了一些点,但不是全部,所以可能会出现无法预料的困难,要么问他们,要么在网上搜索,知道的人编辑发现错误。 ps:如果有机会和愿望的话,最好用现成的方案,意思一样,更适合TK,也就是tables。 资源
应该在发生滚动的 ListBox 上订阅滚动事件,并在该事件的处理程序中,设置其余列表的TopIndex值(该值应与 main 相同)一)。
在我们的例子中,来自 WinForms 的 ListBox 没有自己的滚动事件,所以我们将改进我们的列表:
显然,现在我们只通过代码添加改进的 ListBox 会更容易。
这很容易完成:
现在让我们订阅该事件:
BetterListBox1_Scroll我们的事件处理程序在哪里:让我们更新其余的列表:
准备好!我亲自检查了一些点,但不是全部,所以可能会出现无法预料的困难,要么问他们,要么在网上搜索,知道的人编辑发现错误。
ps:如果有机会和愿望的话,最好用现成的方案,意思一样,更适合TK,也就是tables。
资源