1

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.

flowlayoutexample

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


    }
Sezer Erdogan
  • 167
  • 1
  • 9
  • 34
  • one of the key reasons this doesn't work is you used = instead of += / -=, but that value property acts strange with += anyway, see my answer below for a complete answer that fixes what you did here :) – chrispepper1989 Dec 18 '17 at 11:42
  • Out of interest why are you doing it in code? why not just use flowLayoutPanel1.AutoScroll = true; flowLayoutPanel1.VerticalScroll.Visible = true; – chrispepper1989 Dec 18 '17 at 11:57
  • 1
    becuase this program will be use touch screen machine.So I have to put large button for scrolling :) – Sezer Erdogan Dec 18 '17 at 11:58

3 Answers3

1

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;
    }
chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
  • hi @chrispepper1989 it is not working ,it giving this problem {"Value of '110' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.\r\nParameter name: Value"} – Sezer Erdogan Dec 18 '17 at 11:41
  • p.s. you might also want to check what your "flowLayoutPanel1.VerticalScroll.LargeChange" is set to. if its set to something to high not only would it go out of the min/max right away but you would never see it move :) set it to 5 or something to begin with – chrispepper1989 Dec 18 '17 at 11:47
  • it is working but I can scroll up to a certain point not more .how can fix this problem ? – Sezer Erdogan Dec 18 '17 at 11:48
  • increase your VerticalScroll maximum :) – chrispepper1989 Dec 18 '17 at 11:48
  • how to increase VerticalScroll Maximum ? – Sezer Erdogan Dec 18 '17 at 11:51
  • flowLayoutPanel1.VerticalScroll.Maximum = newMaximum; (this can also be done in the properties on the form creator). If you want the absolute max you might want something like this to run everytime you add a control flowLayoutPanel1.Controls.Cast().Sum(x => x.Height) / columns – chrispepper1989 Dec 18 '17 at 11:54
  • didn't work for me even with the example in the code, i am placing the flowpanel (autosize, growAndShrink)in a panel control with fixed size. – Smith Dec 28 '17 at 20:47
  • Have you tried it in a simple project with just a flow panel in a form? – chrispepper1989 Dec 28 '17 at 20:52
0

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.

chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
0
flowLayoutPanel1.HorizontalScroll.Value += 60;
flowLayoutPanel1.PerformLayout();
mikus
  • 3,042
  • 1
  • 30
  • 40