10

i have a panel in my winforms and in it i load some usercontrols .

i would like to autoscroll to the bottom of the panel( as my panel fills ) everytime a new usercontrol is added . How can i do so ?

3 Answers 3

23

You can do that by setting the VerticalScroll of the Panel but I think it would be better to use ScrollControlIntoView instead.

private void panel1_ControlAdded(object sender, ControlEventArgs e)
{
    panel1.ScrollControlIntoView(e.Control);
}

Good luck!

1
  • 1
    This seems to work... one thing I found weird though, is While the other controls in the panel move up accordingly, TableLayoutPanels that are children to the scrolling panel do NOT move up. You have to manually move those yourself, programmatically after scrolling. Odd.
    – eidylon
    Jan 14, 2015 at 19:57
22

You could use ScrollControlIntoView and pass the control you last added.

An alternate solution would be:

panel.VerticalScroll.Value = panel.VerticalScroll.Maximum
2
  • 9
    Anyone trying this solution who has the problem of the scrollbars not updating, you need to call PerformLayout() after setting Value. Problem and solution confirmed here: stackoverflow.com/questions/5565653/…
    – Rob
    Mar 25, 2012 at 22:53
  • Notice, If you adding usercontrolls dynamicly you have to also update the value of panel.VerticalScroll.Maximum manually.
    – Darkhan ZD
    Jan 17, 2020 at 4:42
0

I found that continuously adding controls to the panel at vertical increments would be affected negatively whenever a user had scrolled the panel up or down. I used the tip from Homam above, and found the following to work well:

panel1.VerticalScroll.Value = 0;

// Creating and adding a TextBox, tb, to the panel

panel1.ScrollControlIntoView(tb);

So first, I scroll to the top in order to use absolute vertical positions for my text boxes, then I place the Text Box, and finally, I make sure that the newly created text box comes into view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.