C#TableLayoutPanel第一列是错误的地方

时间:2015-12-30 13:29:25

标签: c# user-interface designer

所以我尝试使用3列和5行创建TableLayoutPanel。当我运行此代码时,表格如下所示:

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

2 个答案:

答案 0 :(得分:0)

首先,您不需要定义行数和列数,它们会自动添加。第二,你从第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);

还要在循环中更新代码。

制作表格应如下所示:

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

答案 1 :(得分:0)

这是修复。 当你从1开始时,列索引从0开始。

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

如果你想象你的表格布局是3,3格。然后0,0将引用第一行和第一列 enter image description here