Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding Up Slow, CPU-Intensive Scrolling in WinForms

How can I speed up the scrolling of UserControls in a WinForms app.?

My main form has trouble scrolling quickly on slow machines--painting for each of the small scroll increments is CPU intensive.

My form has roughly fifty UserControls (with multiple fields) positioned one below the other. I’ve tried intercepting OnScroll and UserPaint in order to eliminate some of the unnecessary re-paints for very small scroll events, but the underlying Paint gets called anyway.

How can I streamline scrolling on slower machines?

like image 983
S B Avatar asked May 03 '10 15:05

S B


3 Answers

The tried-and-true method is to use an offscreen bitmap which is updated only when the data represented by your control actually changes; then, all OnPaint needs to do is render that bitmap to the screen.

If your paint process is intensive, and since you have so many controls, you'll find this makes a massive difference to the performance of your application.

Note that using the DoubleBuffering control property won't help in your case--it does tell WinForms to render to an offscreen bitmap before rendering to the screen, but that still happens at every paint cycle since WinForms doesn't keep track of when the representation has changed.

So, you'd have to roll your own. It's not that difficult. Here's what looks like a reasonably good article on the subject.

like image 162
Ben M Avatar answered Nov 13 '22 03:11

Ben M


You can also increase the size of the scroll step. For example

panel1.VerticalScroll.SmallChange = 100;

Will cause the panel to scroll it's content 100 units vertically per click of the scrollbar button. So you take bigger steps each time, that might make the experience feel better at least. And you can do the same for the horizontal scroll bar of course.

like image 20
Chris Taylor Avatar answered Nov 13 '22 04:11

Chris Taylor


I have used tabs to eliminate scrolling.

like image 38
Raj More Avatar answered Nov 13 '22 02:11

Raj More