【问题标题】:how to create custom scroll button for flow layout panel in c#如何在 C# 中为流布局面板创建自定义滚动按钮
【发布时间】:2018-05-31 17:26:32
【问题描述】:

我必须为流布局面板项目创建向上和向下按钮垂直滚动。我该怎么做?我会为 POS 做这个表格。

我这样做了,但它不起作用:我有很多按钮,它们的高度为 87:我添加了代码和图片。

    private void btnScrollUp_Click(object sender, EventArgs e)
    {


        flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.LargeChange-1 ;
        flowLayoutPanel1.PerformLayout();



    }

    private void btnScrollDown_Click(object sender, EventArgs e)
    {


        flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.LargeChange+ 1;
        flowLayoutPanel1.PerformLayout();


    }

【问题讨论】:

  • 这不起作用的一个关键原因是您使用了 = 而不是 += / -=,但是无论如何,该 value 属性对 += 的行为很奇怪,请参阅下面的答案以获得完整的答案修复你在这里所做的事情:)
  • 出于兴趣,您为什么要在代码中这样做?为什么不直接使用 flowLayoutPanel1.AutoScroll = true; flowLayoutPanel1.VerticalScroll.Visible = true;
  • 因为这个程序将使用触摸屏机器。所以我必须放大按钮来滚动:)

标签: c# winforms flowlayoutpanel


【解决方案1】:

或者,您可能只想将“AutoScroll”设置为 false,以下代码实现了正确的编程滚动:

 public Form1()
    {
        InitializeComponent();
        flowLayoutPanel1.AutoScroll = false;

    }

    public int scrollValue = 0;
    public int ScrollValue
    {
        get
        {


            return scrollValue;
        }
        set
        {
            scrollValue = value;

            if (scrollValue < flowLayoutPanel1.VerticalScroll.Minimum )
            {
                scrollValue = flowLayoutPanel1.VerticalScroll.Minimum;
            }
            if (scrollValue > flowLayoutPanel1.VerticalScroll.Maximum)
            {
                scrollValue = flowLayoutPanel1.VerticalScroll.Maximum;
            }

            flowLayoutPanel1.VerticalScroll.Value = scrollValue;
            flowLayoutPanel1.PerformLayout();

        }
    } 
    private void Add_Control(object sender, EventArgs e)
    {
        flowLayoutPanel1.Controls.Add(new Button(){Width = flowLayoutPanel1.Width, Height = 87});
    }

    private void UpClick(object sender, EventArgs e)
    {
        ScrollValue -= flowLayoutPanel1.VerticalScroll.LargeChange;

    }

    private void DownClick(object sender, EventArgs e)
    {
        ScrollValue += flowLayoutPanel1.VerticalScroll.LargeChange;
    }

【讨论】:

  • 嗨@chrispepper1989 它不起作用,它给出了这个问题{“'110' 的值对'值'无效。'值'应该在'最小值'和'最大值'之间。\ r\n参数名称:值"}
  • 附注您可能还想检查“flowLayoutPanel1.VerticalScroll.LargeChange”的设置。如果将其设置为高值,它不仅会立即超出最小值/最大值,而且您永远不会看到它移动:) 将其设置为 5 或开头的值
  • 它正在工作,但我可以向上滚动到某个点而不是更多。如何解决这个问题?
  • 增加你的 VerticalScroll 最大值:)
  • 如何增加 VerticalScroll 最大值?
【解决方案2】:

你想实现什么类型的滚动,这段代码你会吗?

How to Programmatically Scroll a Panel

这允许您滚动每个控件,而不是“平滑”滚动,但我认为这适用于您的应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    相关资源
    最近更新 更多