Wednesday, March 20, 2019

AutoScrollPosition coordinates need to be negated when restored

One of my Windows Forms applications has a Panel that auto-scrolls to contain many dynamically inserted controls. Sometimes its controls are cleared and regenerated. I want that to be seamless for the user, so the scroll position should be preserved. I tried recording the Value of HorizontalScroll and VerticalScroll and restoring those afterward, and that partially worked but didn't render the scrollbars in the correct place. I also tried saving/restoring AutoScrollPosition but that just left the scroll view in the upper left. I eventually found a Microsoft forum thread somewhere that said AutoScrollPosition is the property to use in this case, but the point set after the reset must be the negation (both X and Y coordinates made negative) of the original point. I'm not sure why that works, but it does.

No comments:

Post a Comment