简体   繁体   English

在运行时期间动态地向TableLayoutPanel添加控件

[英]Adding controls to TableLayoutPanel dynamically during runtime

I have a TableLayoutPanel starting with two columns and 0 rows. 我有一个TableLayoutPanel,以两列和0行开头。 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: 在Form1中,我以这种方式创建TableLayout:

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. 之后在运行时我会得到我需要多少行,如果它们将填充Panel或某些Label。 It might happen that in the same row left will be Panel, right Label etc.. 可能会发生在同一行中,将是Panel,right Label等。

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. 您无需定义行数和列数,它们将自动添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM