I am working on a Form that takes and draws a point on a mouse click. I am confused about how to properly get and add the scrolling offset so that the points can be drawn correctly. For example, right now when I add a point where the upper left coordinate is (0,0), the point redraws itself and moves with the scrolling action instead of staying at the spot it was originally created at. I have set
this.AutoScroll = true
and have set the minimum size manually
this.AutoScrollMinsSize = new Size(800,600);
Here is what my mouse click event looks like so far:
if (e.Button == MouseButtons.Left)
{
Point newPoint = new Point(e.X, e.Y);
p.X += this.AutoScrollOffset.X;
p.Y += this.AutoScrollOffset.Y;
this.Invalidate();
}
What is the proper way to use the AutoScrollOffset property to keep the points where they belong instead of moving as I scroll?
I should add that my program also overrides the Scroll event to repaint when a scroll event occurs to fix the problem of a drawing disappearing once the visible area was left.
AutoScrollOffset is not the correct property to use. It has very limited use, it can apply an offset to the scroll position when the ScrollControlIntoView() method is used. Which is pretty rare, never once used it myself.
You need to use the AutoScrollPosition property instead:
if (e.Button == MouseButtons.Left) {
var newPoint = new Point(e.X - this.AutoScrollPosition.X,
e.Y - this.AutoScrollPosition.Y);
// etc..
}
Note that substraction is required, a bit unintuitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With