Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating Table Layout Panel takes too long

Tags:

c#

winforms

I am working on a WinForms application in C#. I initially have a Table Layout Panel which I populate with dynamically created buttons, one for each cell. This works well, and quite fast. During the program execution, I have to clear this Table Layout Panel and repopulate it, pretty much with the same display, but more buttons (twice the number from the initial table). The problem is that this process takes a lot of time (over 10 seconds). Am I doing something wrong? This is the code:

buttonTable.Controls.Clear();
buttonTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
buttonTable.RowCount = GetNoOfLines();
buttonTable.ColumnCount = GetNoOfLines();

for (int z = 1; z <= GetNoOfLines(); z++)
{
    buttonTable.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 25));
    buttonTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
}

for (int i = 1; i <= GetNoOfLines(); i++)
{
    for (int j = 1; j <= GetNoOfLines(); j++)
    {
        FieldButton tempButton = new FieldButton(i, j, GetNoOfLines());
        tempButton.MouseDown += new MouseEventHandler(OnFieldButtonClicked);

        buttonTable.Controls.Add(tempButton, j - 1, i - 1);
     }
 }

Note: FieldButton is a class derived from Button, to which I've added two int's, nothing special about it. Also, the buttons are added correctly to the table. Thanks!

like image 638
Adrian Marinica Avatar asked Jan 21 '26 19:01

Adrian Marinica


1 Answers

I had problems with TableLayoutPanel performance until I read this thread and created a custom control that set the DoubleBuffered property.

public class DoubleBufferedTableLayoutPanel :TableLayoutPanel
{
    public DoubleBufferedTableLayoutPanel()
    {
        DoubleBuffered = true;
    }
}

Try that control instead of the standard TableLayoutPanel. I second the advice to suspend and resume layout while you're populating the table.

like image 179
Jamie Ide Avatar answered Jan 23 '26 09:01

Jamie Ide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!