Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding controls to TableLayoutPanel dynamically during runtime

I have a TableLayoutPanel starting with two columns and 0 rows. What I need to do is, dynamically adding a row and filling both of the columns with different controls (it will be panels). In Form1 I am creating the TableLayout this way:

TableLayoutPanel Table = new TableLayoutPanel(); Table.Location = new Point(10, 40); Table.Size = new Size(620,100); Table.AutoSize = true; Table.Name = "Desk"; Table.ColumnCount = 2; Table.RowCount = 0; Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddRows; this.Controls.Add(Table); 

afterwards during runtime I am getting how many rows will I need, and if they will be filled with either a Panel or some Label. It might happen that in the same row left will be Panel, right Label etc..

like image 289
Marek Buchtela Avatar asked Dec 21 '12 15:12

Marek Buchtela


People also ask

How to add control in TableLayoutPanel c#?

You can add controls to a TableLayoutPanel by dragging and dropping control to the TableLayoutPanel. We can add controls to a TableLayoutPanel at run-time by using its Add method. ColumnStyle and RowStyle properties are used to specify number of columns and rows in a TableLayoutPanel.

How to use TableLayoutPanel in Windows Form?

Drag a TableLayoutPanel control from the Toolbox onto your form. Note that, by default, the TableLayoutPanel control has four cells. Drag a Button control from the Toolbox into the TableLayoutPanel control and drop it into one of the cells. Note that the Button control is created within the cell you selected.


1 Answers

Use something like this:

Table.Controls.Add(new Label { Text = "Type:", Anchor = AnchorStyles.Left, AutoSize = true }, 0, 0); Table.Controls.Add(new ComboBox { Dock = DockStyle.Fill }, 0, 1); 

You don't need to define number of rows and columns, they will be added automatically.

like image 50
VladL Avatar answered Sep 21 '22 15:09

VladL