How To Create a Flicker Free TableLayoutPanel
The TableLayoutPanel that comes with .NET 2.0 is handy for doing simple HTML style layouts without the hassle of having to embed a browser control in your form and write the HTML. The control is designed to be a lightweight container (not as powerful as the DataGridView, but more flexible in it's approach) and you can embed any control you like in it.
The main problem however is that the control flickers incredibly badly when it gets resized.
For windows forms applications the way to remove flickering is to enable double buffering. For a form you can just set the DoubleBuffered property to true, and while this will reduce flicker when you resize the form itself, the TableLayoutPanels on the form still flicker as they resize.
So to fix this you just need to turn on double buffering for the control. Unfortunately, the control doesn't feature a "DoubleBuffered" property.
You could try setting the ControlStyles for the control as well, however the SetStyle method is not exposed by the control.
So that means we'll need to use subclassing to set the double buffering flags. The following C# code shows you a simple double buffered TableLayoutPanel :
using System.ComponentModel;
using System.Windows.Forms;
namespace MyNameSpace
{
/// <summary>
/// Double Buffered layout panel - removes flicker during resize operations.
/// </summary>
public partial class DBLayoutPanel : TableLayoutPanel
{
public DBLayoutPanel()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint, true);
}
public DBLayoutPanel(IContainer container)
{
container.Add(this);
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint, true);
}
}
}
Create the class as shown, rebuild your code and the toolbox should now show you that a DBLayoutPanel control is available for your UI pleasure.
Oh, if you happen to have already built forms using the standard TableLayoutPanel you won't need to delete them and start again. Just go into the *.Designer.cs code-beside files and change the TableLayoutPanel references to DBLayoutPanel ones (watch your namespaces!). Rebuild your application and everything should run as it did before, this time without the flickering.