How to Programmatically Scroll a Panel

后端 未结 7 2054
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 06:57

I have a System.Windows.Forms.Panel with some content.

I am trying to programmatically scroll the panel (vertically) either up or down.

I have t

7条回答
  •  既然无缘
    2020-11-29 07:30

    If you have a class that derives from Panel, then call these two protected methods to scroll the panel:

    // The bottom is off screen; scroll down. These coordinates must be negative or zero.
    SetDisplayRectLocation(0, AutoScrollPosition.Y - item.BoundingRect.Bottom + ClientRectangle.Bottom);
    AdjustFormScrollbars(true);
    

    In my example, item.BoundingRect.Bottom is the Y coordinate of the bottom of a thumbnail, and I need to scroll the panel down so that the whole thumbnail is visible.

    @King King's solution of creating a temporary Control just so that scrolling could be done seemed "heavy" to me. And @Hans Passant's suggestion of setting AutoScrollMinSize and AutoScrollPosition didn't work for me.

    Leave AutoScroll to its default value of 'true'.

提交回复
热议问题