Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoScrollPosition does not report the correct value after setting it to a point location

I have a user control, which is scrollable control, and I want to change its AutoScrollPosition (only the X value).

I'm doing it like this:

int newScrollX = myFunction();
Point p = new Point(newScrollX, this.AutoScrollPosition.Y);
this.AutoScrollPosition = p;

newScrollX gets the correct value, p gets the correct point, but after the line of AutoScrollPosition setting, the AutoScrollPosition is (0,0).

What is the problem?

Thanks

like image 442
user1439691 Avatar asked Oct 24 '12 16:10

user1439691


1 Answers

AutoScrollPosition is quite cumbersome.

usually you get negative values when doing this:

Point p = this.AutoScrollPosition;

but when setting the scroll position you have to use positive values... so to restore the exact same scroll position you have to invert the negative numbers:

this.AutoScrollPosition = new Point(-p.X, -p.Y)

Otherwise the AutoScrollPosition will be 0,0 as observed.

like image 134
TomSmartBishop Avatar answered Nov 15 '22 16:11

TomSmartBishop