-->

How to avoid flickering in TableLayoutPanel in c#.

2019-02-13 10:10发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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; }
        }
}


回答4:

This code submitted on another question nailed it, it uses an API call to set the TableLayoutPanel redraw.

https://stackoverflow.com/a/10038782/197765



回答5:

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();