Pause Windows Form Redrawing

I have a window shape. It contains several datagridviews on it. At some point, the user may click a button that updates datagridviews. When they do this, they can usually sit and watch the datagridview redraw, one row at a time. I would like the control to not draw until the "done" is executed, i.e. I would like to indicate management

Control.SuspendRedraw()
this.doStuff()
this.doOtherStuff()
this.doSomeReallyCoolStuff()
Control.ResumeRedaw()

I saw the SuspendLayout / ResumeLayout functions, but they do nothing (they seem to be more related to resizing / moving controls, not just editing their data)

+5
source share
2

, :

DoubleBuffer DataGridView true. DataGridView, . , , .

class CustomDataGridView: DataGridView
{
    public CustomDataGridView()
    {
        DoubleBuffered = true;
    } 
}

, DataGridView , , .


, - Win32 WM_SETREDRAW

// ... this would be defined in some reasonable location ...

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

static void EnableRepaint(HandleRef handle, bool enable)
{
    const int WM_SETREDRAW = 0x000B;
    SendMessage(handle, WM_SETREDRAW, new IntPtr(enable ? 1 : 0), IntPtr.Zero);
}

HandleRef gh = new HandleRef(this.Grid, this.Grid.Handle);
EnableRepaint(gh, false);
try
{
    this.doStuff();
    this.doOtherStuff();
    this.doSomeReallyCoolStuff();
}
finally
{
    EnableRepaint(gh, true);
    this.Grid.Invalidate(); // we need at least one repaint to happen...
}
+8

DoubleBuffer. Form.DoubleBuffer true, .

0

All Articles