开发者

Dynamically creating Table Layout Panel takes too long

开发者 https://www.devze.com 2023-03-16 16:01 出处:网络
I am working on a WinForms application in C#. I initially have a Table Layout Panel which I populate with dynamically created buttons, one开发者_如何学JAVA for each cell. This works well, and quite fa

I am working on a WinForms application in C#. I initially have a Table Layout Panel which I populate with dynamically created buttons, one开发者_如何学JAVA 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!


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.


Detach buttonTable from the form and attach just after you are done adding controls to it. Works fine that way.


Try using SuspendLayout() and ResumeLayout() before and after your code adding the buttons to decrease the time taken to repaint the buttons. This should decrease some time

0

精彩评论

暂无评论...
验证码 换一张
取 消