13

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.

Jmnstr
  • 80
  • 13
Vyasdev Meledath
  • 8,926
  • 20
  • 48
  • 68

7 Answers7

24

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.

Ian
  • 33,605
  • 26
  • 118
  • 198
10

This worked great for me Remove flickering due to TableLayoutPanel & Panel in windows form

Here what's in that link (copied verbatim)

Completely Remove flickering due to TableLayoutPanel & Panel in windows form go as follows:=- 1. Set double buffered property of Form =true. 2. Paste Following 2 functions in form.cs

#region .. Double Buffered function ..
   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);
    }

   #endregion


   #region .. code for Flucuring ..

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

    #endregion
  1. Call SetDoubleBuffered(“TableLaoutPannel_controlName”) for each TableLayoutPannel,Pannel, Splitcontainer, Datagridview & all container controls.

Thanks to RhishikeshLathe Posted 16-Feb-14 20:11pm

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36
  • 1
    Added a quote block there. However note that adding just a link to the other post and copying all the contents is not a proper attribution, per se. If you have nothing to add apart from the details in the link, then it's always better to leave them as comments. Take care the next time. – Bhargav Rao Nov 11 '16 at 07:50
10

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.

  • 2
    How does this answer the question? – Bill Tür stands with Ukraine Apr 25 '19 at 13:37
  • add it to the bottom of affected class and I assure you it will work. It essentially awaits the full render of the Winform. If you havent tested it please dont disregard. I had a huge issue with winform latency and this fixed it. –  May 22 '19 at 10:06
  • This answers the question incredibly well, flickering is resolved across the whole form, thanks for this post. – Daniel Arena Jun 24 '20 at 11:25
1

Use this panel to set the property dBuffer true

public partial class MyTableLayoutPanel : TableLayoutPanel
{
        public MyTableLayoutPanel()
        {
            InitializeComponent();
        }

        public MyTableLayoutPanel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        /// <summary>
        /// Double buffer
        /// </summary>
        [Description("Double buffer")]
        [DefaultValue(true)]
        public bool dBuffer
        {
            get { return this.DoubleBuffered; }
            set { this.DoubleBuffered = value; }
        }
}
uzoom
  • 11
  • 1
1

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.

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

-1

As an improvement of the above, I had better results with:

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

    // add controls

    panel.ResumePaint();
    panel.ResumeLayout();
Garrett
  • 7
  • 1
  • 8
    `StopPaint` and `ResumePaint` are not native methods of the TableLayoutPanel control. You are obviously using an extension, which you didn't include in your answer. – LarsTech Oct 18 '13 at 14:29