简体   繁体   中英

how to create custom scroll button for flow layout panel in c#

I have to create up and down button vertical scrolling for flow layout panel items.How can I do? I will do this form for POS.

I done this way but it is not working: I have lots of buttons they have size 87 height: I added code and picture.

流布局示例

    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();


    }

Alternatively you might just want to set "AutoScroll" to false the following code implements proper programmatic scroll:

 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;
    }

What type of scroll are you trying to achieve, would this code do you?

How to Programmatically Scroll a Panel

This allows you to scroll per control, rather than a "smooth" scroll but I think this would work for your application.

        flowLayoutPanel1.HorizontalScroll.Value    += 60;
        flowLayoutPanel1.PerformLayout();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM