tags:

views:

138

answers:

4

Is there a way to defer a form from updating/repainting while manipulating controls?

Edit:

I am using TableLayoutPanel control on my form, and I want to remove/add different user controls into different cells of the table dynamically at runtime.

when I do this the screen update is too slow, and there is some blinking action happening.

tablelayout.SuspendLayout()

SuspendLayout seems to have no effect.

+1  A: 

It depends on the control; look for BeginUpdate / EndUpdate or BeginEdit / EndEdit, etc (and use them in a try/finally block). Note, however, that only some controls have these methods, and they aren't bound to a handy interface so it is a bit manual. For example:

    listView.BeginUpdate();
    try
    {
        for (int i = 0; i < 100; i++)
        {
            listView.Items.Add("Item " + i);
        }
    }
    finally
    {
        listView.EndUpdate();
    }

Note that data-bound controls may have other mechanisms for disabling data-related changes; for example, BindingList<T> has a settable RaiseListChangedEvents that turns off change-notifications while you edit data. You might also look at things like "virtual mode".

Marc Gravell
A: 

You could try SuspendLayout / ResumeLayout - this is how the code generated by the forms designer does it - take a look inside the InitializeComponents method in the code behind for a form to see what I mean.

matt
+1  A: 

Try:

try {
    SuspendLayout();

    // manipulate your controls

} finally {
    ResumeLayout();
}
Dan Tao
A: 

I completely got rid of the flicker by creating a class that inherited the table, then enabled doublebuffering.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace myNameSpace.Forms.UserControls
{
    public class TableLayoutPanelNoFlicker : TableLayoutPanel
    {
        public TableLayoutPanelNoFlicker()
        {
            this.DoubleBuffered = true;
        }
    }
}
Kevin