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.
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".
Try:
try {
SuspendLayout();
// manipulate your controls
} finally {
ResumeLayout();
}
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;
}
}
}
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.
精彩评论