1
Reply

C# - Graphics on custom panel control is distorted (smeared) when auto scrolled

Aidan Follestad

Aidan Follestad

Jan 30 2011 2:07 AM
3.9k
Hello,

I have a custom panel control I'm using to make a contact list, and everything works perfectly except one thing:

I set autoscroll to true, and then have the AutoScrollMinSize property set to the number of items times the item height to get how much space all items will take up in points; that works, except once the user drags the scrollbar up or down, the graphics in the panel become distored and it appears that someone smeared paint all over the control.

Any ideas? I already have the OptimizedDoubleBuffer and ResizeRedraw styles set, that helps with when the user resizes the form containing the control, but it doesn't fix the panel graphics distortion issue.

*EDIT:
I found a solution; in the OnPaint override method, before you begin to draw to the panel graphics, you add this code: 

e.Graphics.Transform = new Matrix(1, 0, 0, 1, this.AutoScrollPosition.X, this.AutoScrollPosition.Y);

This maintains the scroll position during painting, and allows smooth scrolling with autoscroll enabled.

Answers (1)