TableLayoutPanel reacts very slowly to events

In my application, I really needed to place a lot of controls (label, textbox, domainupdown) in good order. So I went ahead and used a few nested TableLayoutPanel . The problem is that this form reacts very slowly to most events (resizing, maximizing, minimizing and ...) so that the controls in the tables change in size up to 5 seconds, redraw to the new size of the form.

Now I put a finger on my eye! If this form is slow on my home PC (i7 @ 4GHz and a good graphics card), what will it do for the old P4 computer at work?

I even tried using the code below, but it does absolutely nothing unless it slows it down anymore!

 private void FilterForm_ResizeBegin(object sender, EventArgs e) { foreach(TableLayoutPanel tlp in panelFilters.Controls) { if(tlp != null) { tlp.SuspendLayout(); } } } private void FilterForm_ResizeEnd(object sender, EventArgs e) { foreach (TableLayoutPanel tlp in panelFilters.Controls) { if (tlp != null) { tlp.ResumeLayout(); } } } 

Please let me know if there is a trick to get the tablelayoutpanel to work faster ... or if you know a better approach to stack about a hundred controls that are evenly aligned.

+4
performance c # winforms tablelayoutpanel
source share
2 answers

Use this code.

 public class CoTableLayoutPanel : TableLayoutPanel { protected override void OnCreateControl() { base.OnCreateControl(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.CacheText, true); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= NativeMethods.WS_EX_COMPOSITED; return cp; } } public void BeginUpdate() { NativeMethods.SendMessage(this.Handle, NativeMethods.WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero); } public void EndUpdate() { NativeMethods.SendMessage(this.Handle, NativeMethods.WM_SETREDRAW, new IntPtr(1), IntPtr.Zero); Parent.Invalidate(true); } } public static class NativeMethods { public static int WM_SETREDRAW = 0x000B; //uint WM_SETREDRAW public static int WS_EX_COMPOSITED = 0x02000000; [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); //UInt32 Msg } 
+18
source share

If you create a new class derived from TableLayoutPanel and set ControlStyles so that DoubleBuffered true, your performance will improve dramatically.

 public class MyPanel : TableLayoutPanel { public MyPanel() { this.SetStyle(ControlStyles.DoubleBuffer, true); } } 
0
source share

All Articles