views:

152

answers:

2

I am adding a couple hundred controls to a form and the form flickers until its done as it adds each control, is there anyway to stop this?

+2  A: 

might want to surround your code with SuspendLayout and ResumeLayout properties of the Form

this.SuspendLayout();

//create controls

this.ResumeLayout(true);

dotnetNinja
+3  A: 

The answer is the same as the answer to this question:

http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children

(Answer copied for convenience: originally from: http://stackoverflow.com/users/36860/ng5000)

At my previous job we struggled with getting our rich UI app to paint instantly and smoothly. We were using standard .Net controls, custom controls and devexpress controls.

After a lot of googling and reflector usage I came across the WM_SETREDRAW win32 message. This really stops controls drawing whilst you update them and can be applied, IIRC to the parent/containing panel.

This is a very very simple class demonstrating how to use this message:

class DrawingControl
{
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private const int WM_SETREDRAW = 11; 

    public static void SuspendDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
    }

    public static void ResumeDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
        parent.Refresh();
    }
}

There are fuller discussions on this - google for C# and WM_SETREDRAW, e.g.

C# Jitter

Suspending Layouts

GWLlosa