title: Fast way to add Controls to a TableLayoutPanel
body: <p>I have to add a lot of <code>Control</code> objects to a <a href="https://msdn.microsoft.com/de-de/library/system.windows.forms.tablelayoutpanel%28v=vs.110%29.aspx" rel="nofollow"><code>TableLayoutPanel</code></a> dynamically, what takes a significant amount of time. I also need to be able to access the controls via row- and column-index of the <code>TableLayoutPanel</code> and vice versa.</p>
  
  <p><code>TableLayoutPanel.Controls</code> has (as far as I know) 3 ways to add <code>Control</code> objects:  </p>
  
  <ul>
  <li><code>.Add(Control)</code> - inherited, position is -1,-1 with <code>.GetCellPosition(Control)</code>  </li>
  <li><code>.Add(Control, column, row)</code> - position and indexes are correct, but maybe a bit slow?</li>
  <li><code>.AddRange (Control[])</code> - inherited, faster, shown position is correct (every cell is filled, if necessary columnspans are set afterwards), but position is -1,-1 with <code>.GetCellPosition(Control)</code>  </li>
  </ul>
  
  <p>Is there a way to combine the advantages of <code>.Add(Control, column, row</code> and <code>.AddRange(Control[])</code>, i.e. to add a lot of <code>Control</code>objects fast to a <code>TableLayoutPanel</code> while still being able to get the position of a <code>Control</code> programmatically?</p>
  
  <hr>
  
  <p><strong>EDIT</strong> to include some information from the comments:</p>
  
  <ul>
  <li>There are up to 1000 controls added</li>
  <li>I already use <code>SuspendLayout()</code> and <code>ResumeLayout()</code>  </li>
  <li>The <code>TableLayoutPanel</code> takes about 2 seconds to load. According to the profiler roughly 50% of the time is spent with adding Controls, 20% with <code>ResumeLayout()</code></li>
  </ul>
  
  <hr>
  
  <p><strong>EDIT: MCVE</strong><br>
  My original code is more complex, but this is an example of a <code>TableLayoutPanel</code> where adding the controls takes most of the time spent (2/3). I am searching for a way to speed this up.  </p>
  
  <pre><code>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&lt;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();
      }
  }
  </code></pre>