You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用左右两个按钮实现Panel的分段滚动?

分段滚动按钮控件的逻辑异常问题

现有问题表现

  • 显示第11-15个按钮时,仅显示「<」按钮(点击「»」后无更多内容);
  • 显示第6-10个按钮时,「<」和「›」按钮同时可见;
  • 显示第1-5个按钮时,仅显示「›」按钮。

期望效果

以15个按钮(屏幕仅能显示5个)为例:点击「»」一次显示第6-10个按钮,再点击一次显示第11-15个按钮,仅需2次点击即可查看全部按钮;同时滚动按钮的显示逻辑需匹配当前可见段落——在最后一段时隐藏「»」,在第一段时隐藏「<」。

当前实现代码

partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // 必须禁用AutoScroll,否则无法关闭水平滚动条
        Panel.AutoScroll = false;

        // 禁用水平滚动条
        Panel.HorizontalScroll.Enabled = false;
    }

    public int scrollValue = 0;

    public int ScrollValue
    {
        get
        {
            return scrollValue;
        }
        set
        {
            scrollValue = value;

            if (scrollValue < Panel.HorizontalScroll.Minimum)
            {
                scrollValue = Panel.HorizontalScroll.Minimum;
            }

            if (scrollValue > Panel.HorizontalScroll.Maximum)
            {
                scrollValue = Panel.HorizontalScroll.Maximum;
            }

            Panel.HorizontalScroll.Value = scrollValue;
            Panel.PerformLayout();
        }
    }

    private void btnleft_Click(object sender, EventArgs e)
    {
        ScrollValue -= Panel.HorizontalScroll.LargeChange;
    }

    private void btnright_Click(object sender, EventArgs e)
    {
        ScrollValue += Panel.HorizontalScroll.LargeChange;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Panel.Controls.Add(new Button()
        {
            Width = 67,
            Height = Panel.Height
        });
    }
}

内容的提问来源于stack exchange,提问作者Essami Mohammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:56:29