C# TableLayoutPanel 第一列位置错误

C# TableLayoutPanel first column is at wrong place

所以我尝试创建具有 3 列和 5 行的 TableLayoutPanel。当我 运行 此代码时 table 看起来像这样:

https://www.dropbox.com/s/op7mdo60g4tskx4/table.PNG?dl=0

第一个标签不在正确的位置。这就是问题所在。我该如何解决?

//function that creates new TableLayoutPanel and fill it with labels
 public void makeTable()
    {
        TableLayoutPanel panel = new TableLayoutPanel();
        panel.Top = 100;
        panel.Left = 30;
        panel.ColumnCount = 3;
        panel.Width = 690;
        panel.Height = 275;
        panel.RowCount = 1;
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
        panel.Controls.Add(new Label() { Text = 0 + "" }, 1, 0);
        panel.Controls.Add(new Label() { Text = 0 + "" }, 2, 0);
        panel.Controls.Add(new Label() { Text = 0 + "" }, 3, 0);

        //loop that creates the requiret amount of rows in table
        int i = 0;
        while (i < 4)
        {
            panel.RowCount++;
            panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 1, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 2, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i+1 + "" }, 3, panel.RowCount - 1);
            i++;
        }
        this.Controls.Add(panel);
    }

首先您不需要定义行数和列数,它们会自动添加。其次,您从第 1 列开始。正确的代码是:

panel.Controls.Add(new Label() { Text = 0 + "" }, 0, 0);
panel.Controls.Add(new Label() { Text = 0 + "" }, 1, 0);
panel.Controls.Add(new Label() { Text = 0 + "" }, 2, 0);

同时更新循环中的代码。

使 table 应该如下所示:

public void makeTable()
    {
        TableLayoutPanel panel = new TableLayoutPanel();
        panel.Top = 100;
        panel.Left = 30;
        panel.ColumnCount = 3;
        panel.Width = 690;
        panel.Height = 275;
        panel.RowCount = 1;
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));


        //loop that creates the requiret amount of rows in table
       for (int i = 0; i <= 4; i++)
       {
          panel.Controls.Add(new Label() { Text = i + "" }, 0, i);
          panel.Controls.Add(new Label() { Text = i + "" }, 1, i);
          panel.Controls.Add(new Label() { Text = i + "" }, 2, i);
       }
        this.Controls.Add(panel);
    }

这里是修复。 列索引从 0 开始,而您从 1 开始。

 panel.Controls.Add(new Label() { Text = 01 + "" }, 0, 0);
 panel.Controls.Add(new Label() { Text = 02 + "" }, 1, 0);
 panel.Controls.Add(new Label() { Text = 03 + "" }, 2, 0);

完成修复

 TableLayoutPanel panel = new TableLayoutPanel();
        panel.Top = 100;
        panel.Left = 30;
        panel.ColumnCount = 3;
        panel.Width = 690;
        panel.Height = 275;
        panel.RowCount = 1;
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33F));
        panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
        panel.Controls.Add(new Label() { Text = 01 + "" }, 0, 0);
        panel.Controls.Add(new Label() { Text = 02 + "" }, 1, 0);
        panel.Controls.Add(new Label() { Text = 03 + "" }, 2, 0);

        //loop that creates the requiret amount of rows in table
        int i = 0;
        while (i < 4)
        {
            panel.RowCount++;
            panel.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
            panel.Controls.Add(new Label() { Text = i + 1 + "" }, 0, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i + 1 + "" }, 1, panel.RowCount - 1);
            panel.Controls.Add(new Label() { Text = i + 1 + "" }, 2, panel.RowCount - 1);
            i++;
        }
        this.Controls.Add(panel);

如果您想象您的 table 布局是 3,3 网格。那么 0,0 将引用第一行和第一列