tags:

views:

573

answers:

7

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 I can't find it. Please help.

+3  A: 

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

gil
+1  A: 

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

Romain Verdier
A: 

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ñó
A: 

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

JPrescottSanders
A: 

Have you tried BackgroundWorker?

Silvercode
+3  A: 

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 );
}

Edit: oops, forgot the "Update" on the Import :}

moobaa
Hmmmm. Interesting.
Tomas Pajonk
Try this syntax instead:private static extern long LockWindowUpdate(IntPtr Handle);and LockWindowUpdate(IntPtr.Zero);
Magnus Johansson
+2  A: 

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

You can also try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace WindowsTest
{
  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();
    }
  }
}
Magnus Johansson