C# 自动调整 tableLayoutPanel 中的行和列的大小
Automatically resize rows and columns in tableLayoutPanel in C#
我创建了一个由 4x4 网格组成的游戏。当玩家获胜时,我想通过将网格扩展到 10x10 来让游戏更难。
我通过添加以下行使表单变大了:
this.Size = new Size(1375, 1375);
tableLayoutPanel 自行调整大小,因为它的 Dock 属性 设置为填充。
然后我把行数和列数改成10:
this.tableLayoutPanel1.RowCount = 10;
this.tableLayoutPanel1.ColumnCount = 10;
我希望每个单元格占space的10%,所以我尝试了:
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F));
但它不起作用:之前的列和行保持其大小,一个新行和一个新列占据了 space 的其余部分,其余的不随便取一个 space。
那么如何重新设置之前单元格的大小,让每个单元格占space的10%?
您需要先清除样式:
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.ColumnStyles.Clear();
整个代码如下所示:
this.Size = new Size(1375, 1375);
this.tableLayoutPanel1.RowCount = 10;
this.tableLayoutPanel1.ColumnCount = 10;
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.ColumnStyles.Clear();
for (int i = 1; i <= this.tableLayoutPanel1.RowCount; i++)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 1));
}
for (int i = 1; i <= this.tableLayoutPanel1.ColumnCount; i++)
{
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
}
我创建了一个由 4x4 网格组成的游戏。当玩家获胜时,我想通过将网格扩展到 10x10 来让游戏更难。
我通过添加以下行使表单变大了:
this.Size = new Size(1375, 1375);
tableLayoutPanel 自行调整大小,因为它的 Dock 属性 设置为填充。
然后我把行数和列数改成10:
this.tableLayoutPanel1.RowCount = 10;
this.tableLayoutPanel1.ColumnCount = 10;
我希望每个单元格占space的10%,所以我尝试了:
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F));
但它不起作用:之前的列和行保持其大小,一个新行和一个新列占据了 space 的其余部分,其余的不随便取一个 space。
那么如何重新设置之前单元格的大小,让每个单元格占space的10%?
您需要先清除样式:
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.ColumnStyles.Clear();
整个代码如下所示:
this.Size = new Size(1375, 1375);
this.tableLayoutPanel1.RowCount = 10;
this.tableLayoutPanel1.ColumnCount = 10;
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.ColumnStyles.Clear();
for (int i = 1; i <= this.tableLayoutPanel1.RowCount; i++)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 1));
}
for (int i = 1; i <= this.tableLayoutPanel1.ColumnCount; i++)
{
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
}