简体   繁体   中英

How to fix scrollbars when the window or container is resized?

Currently in my application I am using the HScrollBar and VScrollBar for panning around in a large image. The part of the image that is shown is based on the scrollbar's Value property. However, when resizing the SplitContainer or resizing the form window the scrollbar's Value does not automatically update and it may render something off-screen.

At this point I noticed that if you clicked the scrollbar's arrow it magically fixes the scrollbar. I was wondering if there was any way to simulate clicking the scrollbar to do this in the Form_Resize and SplitContainer_Resize event handlers but I couldn't find anything.

Having to manually adjust the scrollbar's value in all resizing events is slow, ugly, and doesn't work well. I'd really like for the scrollbar to just automatically fix itself when the window resizes like it does when you click its arrow but I'm not sure how.

Try calling the scrollbar's Invalidate() method in the form's resize event handler:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate.aspx

That should cause it to redraw correctly after the form is resized.

Use something like this:

HScrollBarObject.SetStyle(ControlStyles.ResizeRedraw, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM