tags:

views:

4030

answers:

7

I have a .net app that I've written in c#. On some forms I frequent update the display fields. In some cases every field on the form (textboxes, labels, picturebox, etc) has its value changed. Plus the frequency of the changes could possibly be every second. However, currently there is a horrible flickering everytime the form is updated. How can I stop the flickering? Is there a way to maybe double buffer? Please help!

A: 

You didn't research this well. There is a DoubleBuffered property in every Form. Try setting that to true. If you havn't overloaded anything on the form painting, then everything should work.

Matthew Scharley
+4  A: 

the short answer is

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

the long answer is: see MSDN or google

just for fun, try calling Application.DoEvents() after each element is updated, and see if the problem gets better or worse ;-)

Steven A. Lowe
I'm not sure that will help. It sounds like the problem is with the standard controls are flickering, and OptimizedDoubleBuffer will only help with custom rendered controls.
Cameron MacFarland
@[Cameron MacFarland]: it can't hurt to try...
Steven A. Lowe
A: 

The ghosting is usually caused because you're running in a single thread and it's being held up with the field updates so the paint event doesnt fire. One way to fix this would be to put the heavy lifting in asynchronous methods. This will allow the form to repaint itself and update whatever is needed when they async method calls back.

DaveK
+4  A: 

You could try to call this.SuspendLayout(); before you start your update and this.ResumeLayout(false); when you have finished setting all the values in this way it should prevent the form from writing values one at a time.

FryHard
suspendlayout/resumelayout are for suppressing events related to adding and moving controls
Steven A. Lowe
Correct, the events only prevent layout functions from being called multiple times when updating large groups of controls. As such these functions will speed up layout but will not prevent flicker. Using them in conjunction with Double Buffering will yield best results.
Toji
+1  A: 

You can double buffer almost every windows forms control, although most of the time it requires that you inherit from the desired control and override a protected property. Be cautioned, though, that I've spent quite a bit of time on the same issue and I've yet to fully remove flicker on my more complex forms.

If you want truly flicker-free windows, I suggest looking at WPF.

Toji
+3  A: 

This worked for me.

http://www.syncfusion.com/faq/windowsforms/search/558.aspx

Basically it involves deriving from the desired control and setting the following styles.

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
SetStyle(ControlStyles.DoubleBuffer, true);
Brian Hasden
A: 

i had the same problem with OpenGLES, which is how i found this thread. of course i realize u are not using ogl, but maybe this helps u anyway ;)

protected override void OnPaintBackground(PaintEventArgs e) { }