15

During a complicated update I might prefer to display all the changes at once. I know there is a method that allows me to do this, but what is it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomas Pajonk
  • 5,132
  • 8
  • 41
  • 51

6 Answers6

22

I think this.SuspendLayout() & ResumeLayout() should do it

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
gil
  • 2,248
  • 6
  • 25
  • 31
20

I don't find SuspendLayout() and ResumeLayout() do what you are asking for. LockWindowsUpdate() mentioned by moobaa does the trick. However, LockWindowUpdate only works for one window at a time.

You can also try this:

using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
    private const int WM_SETREDRAW = 11; 

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      SendMessage(this.Handle, WM_SETREDRAW, false, 0);

      // Do your thingies here
      SendMessage(this.Handle, WM_SETREDRAW, true, 0);

      this.Refresh();
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
9

You can use the old Win32 LockWindowUpdate function:

[DllImport("user32.dll")]
private static extern long LockWindowUpdate(long Handle);

try {
    // Lock Window...
    LockWindowUpdate(frm.Handle);
    // Perform your painting / updates...
} 
finally {
    // Release the lock...
    LockWindowUpdate(0);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
moobaa
  • 4,482
  • 1
  • 29
  • 31
  • 5
    Try this syntax instead: private static extern long LockWindowUpdate(IntPtr Handle); and LockWindowUpdate(IntPtr.Zero); – Magnus Johansson Sep 24 '08 at 13:03
  • 5
    LockWindowUpdate is not designed or intended to stop flickering. You should be using `SetWindowRedraw(hwnd, false)`. (http://blogs.msdn.com/b/oldnewthing/archive/2004/06/10/152612.aspx) – Ian Boyd Dec 29 '11 at 00:18
4

Most complex third-party Windows Forms components have BeginUpdate and EndUpdate methods or similar, to perform a batch of updates and then drawing the control. At the form level, there is no such a thing, but you could be interested by enabling Double buffering.

Community
  • 1
  • 1
Romain Verdier
  • 12,833
  • 7
  • 57
  • 77
1

You can use SuspendLayout and ResumeLayout methods in the form or controls while updating properties. If you're binding data to controls you can use BeginUpdate and EndUpdate methods.

Eduardo Campañó
  • 6,778
  • 4
  • 27
  • 24
0

SuspendLayout will help performance if the updates involve changes to controls and layout: MSDN

JPrescottSanders
  • 2,151
  • 2
  • 16
  • 24