lutrarutra
lutrarutra

Reputation: 474

C# TableLayoutPanel first column is at wrong place

So i try to create TableLayoutPanel with 3 columns and 5 rows. When i run this code the table look like this:

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

First label is not in the right place. So that is the problem. How can I fix it?

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

Upvotes: 1

Views: 514

Answers (2)

sm.abdullah
sm.abdullah

Reputation: 1802

here is the fix. column index starts from 0 while you are starting from 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);

complete fix

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

if you imagine your table layout is 3,3 grid. then 0,0 will refer first row and first column enter image description here

Upvotes: 1

Radin Gospodinov
Radin Gospodinov

Reputation: 2323

First you don't need to define number of rows and columns, they will be added automatically. And second you are starting from column 1. The correct code is:

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

Also update your code in the loop.

Make table should look like this:

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

Upvotes: 0

Related Questions