C# 在运行时动态地向 TableLayoutPanel 添加控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13992429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding controls to TableLayoutPanel dynamically during runtime
提问by Marek Buchtela
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:
我有一个以两列和 0 行开头的 TableLayoutPanel。我需要做的是,动态添加一行并用不同的控件填充两列(它将是面板)。在 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. It might happen that in the same row left will be Panel, right Label etc..
之后在运行时我得到了我需要多少行,以及它们是否会填充面板或一些标签。可能会发生在同一行左侧将是面板,右侧标签等。
采纳答案by VladL
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.
您不需要定义行数和列数,它们会自动添加。