将控件添加到 TableLayoutPanel 的快速方法

Bre*_*eze 3 .net c# tablelayoutpanel winforms

我必须动态地添加很多Control对象TableLayoutPanel,这需要花费大量时间。我还需要能够通过 的行和列索引访问控件,TableLayoutPanel反之亦然。

TableLayoutPanel.Controls有(据我所知)3 种添加Control对象的方法:

  • .Add(Control) - 继承,位置为 -1,-1 与 .GetCellPosition(Control)
  • .Add(Control, column, row) - 位置和索引是正确的,但可能有点慢?
  • .AddRange (Control[]) - 继承的,更快的,显示的位置是正确的(每个单元格都被填充,如果有必要,在之后设置列跨度),但位置是-1,-1 .GetCellPosition(Control)

有没有一种方法结合的优势.Add(Control, column, row.AddRange(Control[]),即大量的添加Control对象快的TableLayoutPanel同时仍然能够获得的位置Control编程?


编辑以包含评论中的一些信息:

  • 添加了多达 1000 个控件
  • 我已经使用SuspendLayout()ResumeLayout()
  • TableLayoutPanel需要约2秒至负载。根据分析器,大约 50% 的时间花在添加控件上,20% 的时间花在添加控件上ResumeLayout()

编辑:MCVE
我的原始代码更复杂,但这是TableLayoutPanel添加控件花费大部分时间(2/3)的示例。我正在寻找一种方法来加快速度。

public class FormTLPTest : Form
{
    public FormTLPTest()
    {
        Height = 800;
        Width = 800;

        TableLayoutPanel tlp = new TableLayoutPanel();
        tlp.Dock = DockStyle.Fill;
        tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        tlp.AutoScroll = true;

        Controls.Add(tlp);

        tlp.ColumnCount = 7;
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 80));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 30));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 70));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));

        tlp.SuspendLayout();

        for (int i = 0; i<700; i++)
        {
            Button btn1 = new Button();
            Label lb2 = new Label();
            Label lb3 = new Label();
            Label lb4 = new Label();
            TextBox tb5 = new TextBox();
            Button btn6 = new Button();
            Button btn7 = new Button();

            foreach (Control c in new Control[] { btn1, lb2, lb3, lb4, tb5, btn6, btn7})
            {
                c.Margin = new Padding();
                c.Dock = DockStyle.Fill;
                c.BackColor = Color.White;
            }

            btn1.FlatStyle = FlatStyle.Flat;
            btn6.FlatStyle = FlatStyle.Flat;
            btn7.FlatStyle = FlatStyle.Flat;

            btn1.Text = "1";
            lb2.Text = "Some longer Text - it contains information. Don't know what I should write to fill the space";
            lb3.Text = "Short Text";
            lb4.Text = "Short Text";
            tb5.Text = "5";
            btn6.Text = "Button";
            btn7.Text = "+";

            tlp.Controls.Add(btn1, 0, i);
            tlp.Controls.Add(lb2, 1, i);
            tlp.Controls.Add(lb3, 2, i);
            tlp.Controls.Add(lb4, 3, i);
            tlp.Controls.Add(tb5, 4, i);
            tlp.Controls.Add(btn6, 5, i);
            tlp.Controls.Add(btn7,6, i);
        }

        tlp.ResumeLayout();
    }
}
Run Code Online (Sandbox Code Playgroud)

Iva*_*oev 5

查看参考源,您可以看到该Control.ControlCollection.AddRange方法只不过是一个Add包含在SuspendLayout/ 中的循环ResumeLayout。由于您的代码也包含在此类调用中,因此性能上应该没有区别。

TableLayoutPanel做了两个额外的调用 - SetRowSetColumn,所以我的第一个想法是它们是缓慢的部分。但是,查看源代码(并测量使用或不使用这些调用的时间),当布局引擎暂停时,它们对性能的影响可以忽略不计。

mcve通过根本不使用TableLayoutPanel而只是向表单本身添加控件对您进行了一些额外的测试。结论是 - 你只是有太多的控件。mcve正在创建 4900 个控件。这对于 WinForms(以及一般的 Windows)来说太过分了。运行它后,我的Windows几乎死了。

因此,无法提高添加控制性能。但这不应该是您主要关心的问题。考虑切换到DataGridView支持更多行数的第三方数据转发器控件或某些第三方数据转发器控件,而无需创建大量控件。