auto sliding of Scrollbars

2019-09-12 11:32发布

问题:

In my Winforms application, i have a User Control which serves as a 'screen' to draw various 2D shapes. i have set its 'AutoScroll' property to true, and scrollbars works fine when you zoom the screen( i.e. User control) Now, when i select any shape ( like rectangle or circle etc) and move it so that it goes beyond visible part of screen, i want respective scroll bars to auto slide in order to keep that shape on the visible area of screen. do i need to set any other property of scrollbar ??

回答1:

I don't think it is possible to achieve that without creating your own method.

You can set your scrollbar positon with:

this.VerticalScroll.Value = Y;

Then you have to find out the position of your Rectangle via:

Rectangle.Location.Y;

So this should work for your vertical scrollbar:

this.VerticalScroll.Value = Rectangle.Location.Y;

horzontal:

this.HorizontalScroll.Value = Rectangle.Location.X;

Combined with a MouseDown-Event it will do the trick.



回答2:

Take a look here at the MSDN documention on exactly what the AutoScroll property is and does. It simply will enable the container to have a virtual size that is larger than its visible boundaries. It doesn't actually do the scrolling for you.

If you want the control to "move" with the user as they drag a shape, you will have to capture that action on your own and manually scroll the control over. I'd suggest starting with the MouseDown and MouseMove events. You'll need some logic to figure out when scrolling is needed and how much to actually scroll.