Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid flickering in TableLayoutPanel in c#.net

I am using a TableLayoutPanel for attendance marking purposes. I have added controls (a Panel and a Label) inside of this TableLayoutPanel and created events for them. In some conditions I have cleared all of the controls and proceeded to bind the same controls in different position of TableLayoutPanel. While re-binding the controls, the TableLayoutPanel flickers and is far too slow in initializing.

like image 582
Vyasdev Meledath Avatar asked Jul 13 '11 10:07

Vyasdev Meledath


2 Answers

Suspend the layout until you've added all your controls on.

TableLayoutPanel panel = new TabelLayoutPanel();
panel.SuspendLayout();

// add controls

panel.ResumeLayout();

Also look at using Double Buffering. You'll have to create a sub-class of the TableLayoutPanel. See an example here.

like image 105
Ian Avatar answered Sep 28 '22 01:09

Ian


//Call this function on form load.
SetDoubleBuffered(tableLayoutPanel1);


public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;
            System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);
            aProp.SetValue(c, true, null);
        }

//Works perfectly the double buffered solution for table layout panel and no flickering happens

like image 31
user10965808 Avatar answered Sep 28 '22 00:09

user10965808