【问题标题】:Automatically resize rows and columns in tableLayoutPanel in C#在 C# 中自动调整 tableLayoutPanel 中的行和列的大小
【发布时间】:2015-08-25 22:01:17
【问题描述】:

我创建了一个由 4x4 网格组成的游戏。当玩家获胜时,我想通过将网格扩展到 10x10 来增加游戏难度。

我通过添加这一行使表单更大:

 this.Size = new Size(1375, 1375); 

tableLayoutPanel 会自行调整大小,因为它的 Dock 属性设置为 fill。

然后我将行数和列数改为10:

this.tableLayoutPanel1.RowCount = 10;
this.tableLayoutPanel1.ColumnCount = 10;

我希望每个单元格占用 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));

但它不起作用:之前存在的列和行保持其大小,一个新行和一个新列占用了其余空间,其余的根本不占用任何空间。

那么如何重置之前的单元格的大小,让每个单元格占用 10% 的空间呢?

【问题讨论】:

    标签: c# .net tablelayoutpanel


    【解决方案1】:

    你需要先清除样式:

        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));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多