Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always Scroll to bottom in vertical scroll bar

Tags:

c#

winforms

I have a flowlayoutpanel in my winform in which the images are added dynamically. I want the vertical scroll bar to always be at the bottom showing the last image added. How can i do that? I have

AutoScroll = true

FLow Direction = Top Down

Wrap Content = False

like image 451
umuieme Avatar asked Dec 04 '22 07:12

umuieme


2 Answers

Scrollable container controls, like FlowLayoutPanel, automatically keep the control with the focus in view. But PictureBox is special, it cannot receive the focus. So you have to help by explicitly asking the FLP to make the added control visible, use its ScrollControlIntoView() method. Like this:

    var pic = new PictureBox();
    //...
    flowLayoutPanel1.Controls.Add(pic);
    flowLayoutPanel1.ScrollControlIntoView(pic);

With the strong advantage that this works for any layout setting you applied to the FLP. You can also tinker with the AutoScrollPosition property, but it is harder to get that right:

    flowLayoutPanel1.AutoScrollPosition = new Point(
        pic.Right  - flowLayoutPanel1.AutoScrollPosition.X, 
        pic.Bottom - flowLayoutPanel1.AutoScrollPosition.Y);
like image 162
Hans Passant Avatar answered Dec 17 '22 14:12

Hans Passant


Try this:

scrollBar.Value=scrollBar.Maximum;

here scrollBar is your ScrollBar control in winform.

For more detail, check this.

like image 21
Tanuj Wadhwa Avatar answered Dec 17 '22 16:12

Tanuj Wadhwa