gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:31:30 25 4
gpt4 key购买 nike

如何为 tablelayoutpanel 设置 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;

最佳答案

您需要将 DockStyle 设置为 Fill

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

this.tableLayoutPanel1.Dock = DockStyle.Fill

关于c# - 如何让表格布局适合 100% 的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com