You are reading AutoScrollPosition should be called DriveTimCrazy. You can leave a comment or trackback this post.
Posted on October 8th, 2006 by Tim.
Categories: Programming, Tim.
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.
1 comment.
Comment on December 17th, 2008.
Thank you! this was exactly my problem!
Comments can contain some xhtml. Names and emails are required (emails aren't displayed), url's are optional.