简体   繁体   中英

TableLayoutPanel with nested autosized user controls performance issue

I'm facing with nasty performance issue when using TableLayoutPanel. I have a simple user control with RadioButton and LinkLabel. Text of LinkLabel is dynamic so entire control have AutoSize property set to true.

Now I have a Panel with AutoScroll set to true and TableLayoutPanel auto sized and with 2 columns inside it. This TableLayoutPanel is populated with above-mentioned user controls:

private void PopulateLocationItemsTable(Control[] Controls)
{
    //Suspend outher Panel and set AutoScroll to false just in case.
    panelLocationItemsTableCountainer.SuspendLayout();
    panelLocationItemsTableCountainer.AutoScroll = false;
    //Suspend TableLayoutPanel
    tableLocationItems.SuspendLayout();
    Controls = Controls.OrderBy(c => c.Text).ToArray();
    //Populate left column
    int verticalPosition = 3;
    int leftColumnControlsNumber = Controls.Length / 2;
    for (int i = 0; i < leftColumnControlsNumber; i++)
    {
       tableLocationItems.Controls.Add(Controls[i], 0,0);
       Controls[i].Location = new Point(10, verticalPosition);
       verticalPosition += 17;
    }
    //Populate right column
    verticalPosition = 3;
    for (int i = leftColumnControlsNumber; i < Controls.Length; i++)
    {
        tableLocationItems.Controls.Add(Controls[i], 0, 1);
        Controls[i].Location = new Point(10, verticalPosition);
        verticalPosition += 17;
    }
    //Resume TableLayoutPanel
    tableLocationItems.ResumeLayout(true);
    //And restore outher Panel state
    panelLocationItemsTableCountainer.AutoScroll = true;
    panelLocationItemsTableCountainer.ResumeLayout(true);
}

The problem is that user controls initially populated in FormLoad event and the Form just hangs for around 10 seconds before it actually appears. This is completely unacceptable for me.

This issue goes away if I set AutoSize property of user control to false. I also was tried to put user controls directly to the outher Panel and it also works fine. The problem is just with TableLayoutPanel. Is anyone faced such issue and found the solution? Of corse I can place my user controls myself directly to the Panel calculating right coordinales but using TableLayoutPanel is a "correct" way for such tasks.

Using the TableLayoutPanel is the right approach but you'll want to think of the columns in that control as static widths. I had an application where I faced almost exactly the same problem using that panel and realized I'd just been looking at it all wrong.

If there are two columns, and my container (eg a form) is 300 pixels wide, then each column is 150 pixels wide (minus padding and stuff) so the controls inside those columns have to react rather than the columns reacting.

The other reason you really need to look at it this way is because the engine doesn't layout everything in memory first (like the WPF framework does) so it's extremely in efficient at its core since it commits the changes immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM