TableLayoutPanel

Within a user control, I am dynamically creating a TableLayoutPanel at runtime and then adding controls to it. Basically, I want it to have 4 columns of equal width (25% each).

I have no problems other than when the table is created, the first column is certainly much wider than 25% (more like 60%) and the remaining columns are of equal width.

My code for creating the table is as follows:

TableLayoutPanel tab = new TableLayoutPanel();

tab.SuspendLayout();

tab.Dock = DockStyle.Top;

ColumnStyle defaultColumn = new ColumnStyle(SizeType.Percent, 25);

tab.ColumnStyles.Add(defaultColumn);

tab.AutoSize = true;

tab.RowCount = 1;

tab.ColumnCount = 4;

RowStyle defaultRow = new RowStyle();

defaultRow.SizeType = SizeType.AutoSize;

tab.RowStyles.Add(defaultRow);

....I then add label controls to the table and then call tab.ResumeLayout(). All i need to fix is the problem of the first column width. Can anyone help

Cheers

James




Answer this question

TableLayoutPanel

  • Adrien Regimbald

    Thanks John, but that didn't really help.

    I have managed to solve the problem though! The solution was to iterate through the columns adding the columnstyle AFTER I had set the column count to 4. My code is now as follows:

    TableLayoutPanel tab = new TableLayoutPanel();

    tab.SuspendLayout();

    tab.BackColor = Color.White;

    tab.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;

    tab.Dock = DockStyle.Top;

    tab.AutoSize = true;

    tab.RowCount = 1;

    tab.ColumnCount = 4;

    RowStyle defaultRow = new RowStyle();

    defaultRow.SizeType = SizeType.AutoSize;

    tab.RowStyles.Add(defaultRow);

    for (int i = 0; i < tab.ColumnCount; i++)

    {

    tab.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));

    }

    Hope this helps anyone with the same problem.

    Cheers

    James



  • DanUp

    Here's a link to a MSDN article which shows how to arrange controls on a windows form using the tablelayoutpanel

    TableLayoutPanel Walkthrough


  • TableLayoutPanel