AutoScrollPosition should be called DriveTimCrazy
I ran into a nasty bug in the .Net framework. I was trying to use a Panel class to scroll some content I have in a form. I also wanted to be able to scroll it with the mousewheel, so this required manually changing the scroll position of the panel. The property for this is AutoScrollPosition. So I assumed that I could simply write this:
void DoScrollDown()
{
Point p = panel.AutoScrollPosition;
p.Y += 10;
panel.AutoScrollPosition = p;
}
Strangely, however, this code did not work. It would scroll my panel alternately up and down. At this point, I was amazingly confused at what could be going wrong. Finally, I realized that somewhere, my values were getting negated. Looking at the source for Panel.AutoScrollPosition using reflector, I found the following code:
public void set_AutoScrollPosition(Point value)
{
if (base.Created)
{
this.SetDisplayRectLocation(-value.X, -value.Y);
this.SyncScrollbars(true);
}
this.scrollPosition = value;
}public Point get_AutoScrollPosition()
{
Rectangle rectangle1 = this.GetDisplayRectInternal();
return new Point(rectangle1.X, rectangle1.Y);
}
So it turns out that the Panel class will leave your values alone when it returns the current position, but negates your values when you set the current position. In other words, the following code will negate the X and Y values of the scroll position:
panel.AutoScrollPosition = panel.AutoScrollPosition
I’m thoroughly annoyed.
About this entry
You’re currently reading “AutoScrollPosition should be called DriveTimCrazy,” an entry on The Bloj
- Published:
- 10.8.06 / 8pm
- Category:
- Programming, Tim
- Tags:
2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]