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.