RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1059423
Accepted
vitek2424
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 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. 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。

    资源

    • 1

相关问题

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5