1

Possible Duplicate:
How do I suspend painting for a control and its children?

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?

Community
  • 1
  • 1
Dested
  • 6,294
  • 12
  • 51
  • 73
  • 1
    A form with hundreds of controls will flicker like a cheap motel no matter what you do. Adding them to a panel, then adding the panel doesn't really solve anything. – Hans Passant Jan 15 '10 at 18:21

3 Answers3

4

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

How do I suspend painting for a control and its children?

(Answer copied for convenience: originally from: https://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

Community
  • 1
  • 1
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
  • I don't know what I am doing wrong, but this does not work for me. I still see the controls painted one-by-one in my dynamically created forms... Tried on the parent form and a some containers like flowLayoutPanel. – Oak_3260548 Apr 16 '18 at 17:40
2

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

this.SuspendLayout();

//create controls

this.ResumeLayout(true);

2

The following is the same solution of ng5000 but doesn't use P/Invoke.

public static class SuspendUpdate
{
    private const int WM_SETREDRAW = 0x000B;

    public static void Suspend(Control control)
    {
        Message msgSuspendUpdate = Message.Create(control.Handle, WM_SETREDRAW, IntPtr.Zero,
            IntPtr.Zero);

        NativeWindow window = NativeWindow.FromHandle(control.Handle);
        window.DefWndProc(ref msgSuspendUpdate);
    }

    public static void Resume(Control control)
    {
        // Create a C "true" boolean as an IntPtr
        IntPtr wparam = new IntPtr(1);
        Message msgResumeUpdate = Message.Create(control.Handle, WM_SETREDRAW, wparam,
            IntPtr.Zero);

        NativeWindow window = NativeWindow.FromHandle(control.Handle);
        window.DefWndProc(ref msgResumeUpdate);

        control.Invalidate();
    }
}
ceztko
  • 14,736
  • 5
  • 58
  • 73