How to avoid flickering in TableLayoutPanel in c#.net

Viewed 20370

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.

7 Answers

VB.net:

   Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H2000000
            Return cp
        End Get
    End Property

C#:

    protected override CreateParams CreateParams
    {
     get
     {
      CreateParams cp = base.CreateParams;
      cp.ExStyle = cp.ExStyle | 0x2000000;
      return cp;
     }
    }

In VB add it to the bottom of affected class and I assure you it will work.

In C# add the property to the top of the class along with your other properties.

It essentially awaits the full render of the Winform, and removes the flickering of the form being painted to the screen. If you havent tested it please dont disregard. I had a huge issue with winform latency and this fixed it.

There is another alternative that I ended up using as quite alot of my UI was using Transparency for background colors. I understand that this significantly degrades performance in WINFORMS. However this isnt the case with WPF applications (not usually visible as a flicker), so a conversion could be beneficial.

//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

Related