如何让表格布局适合100%的容器

bot*_*wer 3 c# tablelayoutpanel

如何为tablelayoutpanel提供100%的宽度,以便填充父容器,并在调整窗口大小时调整表的大小.

我的表单现在看起来像这样:

在此输入图像描述

我想动态添加行,所以结果将是这样的:

在此输入图像描述

如果表适合splitcontainer面板会很好.有人知道怎么做吗?

这是我当前向表中添加行的代码:

tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Outset;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;

tableLayoutPanel1.Controls.Add(new Label() { Text = "first row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "second row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "third row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "4th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "5th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "6th row:", Anchor = AnchorStyles.Left, AutoSize = true });
tableLayoutPanel1.Controls.Add(new Label() { Text = "7th row:", Anchor = AnchorStyles.Left, AutoSize = true });
Run Code Online (Sandbox Code Playgroud)

我可以将列和行设置为100%的宽度或高度,那么我怎样才能将表设置为100%?

// 
// tableLayoutPanel1
// 
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(754, 169);
//this.tableLayoutPanel1.Size = new System.Drawing.Size(100F, 169);//pseudo code
this.tableLayoutPanel1.TabIndex = 0;
Run Code Online (Sandbox Code Playgroud)

Seb*_*Piu 7

您需要将DockStyle设置为Fill

您可以在设计器中使用该Dock属性并从下拉列表中选择"填充"或通过代码执行此操作:

this.tableLayoutPanel1.Dock = DockStyle.Fill
Run Code Online (Sandbox Code Playgroud)