2013-11-21 35 views
1

如何给tablelayoutpanel指定100%的宽度,以便填充父容器并在调整窗口大小时调整表格大小。如何让表格布局适合容器的100%

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

enter image description here

我要动态地添加行,所以结果会有些这样的:

enter image description here

这将是,如果不错桌子将适合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 }); 

我可以设置列和行的宽度或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; 

回答

4

您需要设置DockStyle来填充

您可以使用Dock财产和选择填写从下拉菜单中,或通过代码的设计师做到这一点:

this.tableLayoutPanel1.Dock = DockStyle.Fill 
+0

啊,这是更容易,我认为。谢谢! – botenvouwer

+0

不要忘记将其标记为答案。 Regards – Nath

+0

可以在3分钟内接受答案...... – botenvouwer

-1

对接一个选项,但如果你想以编程方式解决你的问题,你可以尝试首先获得父控制界限。然后你可以设置界限到父控制的界限。

this.tableLayoutPanel1.Width = parentControl.Width; 

this.tableLayoutPanel1.Height= parentControl.Height; 

如果你输入这些命令的方法,你可以从窗体大小改变事件也称之为..