How to add controls dynamically in TableLayoutPanel.

For adding new TableLayoutPanel dynamically , you have to follow the following steps :

1.  First declare the TableLayoutPanel and Panel  variable globally. Like,

TableLayoutPanel tlp;

Panel panel2;

2.  Then write this code into the Form1_Load Event.

tlp = new TableLayoutPanel();
tlp.ColumnCount = 4;                   //Take no of columns you require.
tlp.AutoScroll = true;                   // This property is for displaying scroll bars automatically.
tlp.Width = 790;
tlp.Height = 165;

tlp.Location = new Point(11, 241);     //This is for where in the form you want to put this control.

Label lblqty = new Label();                   // Add Label control to your tablelayoutpanel .
lblqty.Name = “lblqty”;
lblqty.Location = new Point(14, 9);
lblqty.Text = “Quantity”;
tlp.Controls.Add(lblqty, 0,0);               // Add Label control to this panel.

Label lblItems = new Label();                       //Add Another label control to this panel.
lblItems.Name = “lblItems”;
lblItems.Location = new Point(109, 9);
lblItems.Text = “Items”;
tlp.Controls.Add(lblItems, 1,0);

Label lblTax = new Label();
lblTax.Name = “lblTax”;
lblTax.Location = new Point(217, 9);
lblTax.Text = “Tax”;
tlp.Controls.Add(lblTax, 2, 0);

Label lblUPrice = new Label();
lblUPrice.Name = “lblUPrice”;
lblUPrice.Location = new Point(331, 9);
lblUPrice.Text = “Unit Price”;
tlp.Controls.Add(lblUPrice, 3,0);

panel2.Controls.Add(tlp);                            // Add tlp(TableLayoutPanel) to Panel2 control.

3.  Now you can run this code.

You can find one tablelayoutpanel and 4 labels in this control.

 

 

About the Author

avatar