1

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

I am using the 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin
  • 3,574
  • 10
  • 38
  • 43

4 Answers4

1

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Try:

try {
    SuspendLayout();

    // manipulate your controls

} finally {
    ResumeLayout();
}
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1

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
  • 3,574
  • 10
  • 38
  • 43
0

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
  • 118
  • 1
  • 7