Here is a quick tip when doing large updates and adds to the Windows Forms Treeview control. Use the beginupdate() method to “turn off” rendering and then after your changes are complete use the endupdate() method to turn rendering back on which will then render all of your changes.
Sometimes it is necessary to do this for a control that doesn’t have these wonderful methods but no to worry you can easily derive a control, add your own “flag” variable and then just override the OnPaint method and if your variable is set to not render then simply return from OnPaint. I have also added the Begin and EndUpdate methods below for setting the variable. If you want to force the control to repaint then you can call EndUpdate( true ). Here is a sample :
public class FastDrawTreeView : System.Windows.Forms.TreeView
{
private bool _disableRendering = false;
public void BeginUpdate()
{
_disableRendering = true;
}
public void EndUpdate(bool redraw)
{
_disableRendering = false;
if (redraw)
this.Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
if (_disableRendering)
return;
base.OnPaint(e);
}
}