Search code examples
c#winformstablelayoutpanel

TableLayoutPanel new row columns not following set size


I have a TableLayoutPanel with 5 columns.

The widths of the columns have been set to fixed values to match their contents.

I have an add row button with the following code:

    private void addrowButton_Click(object sender, EventArgs e)
    {
        RowStyle temp = configTLP.RowStyles[configTLP.RowCount - 1];
        configTLP.RowCount++;
        configTLP.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height));
        configTLP.Controls.Add(new TextBox(), 0, configTLP.RowCount - 1);
        configTLP.Controls.Add(new TextBox(), 1, configTLP.RowCount - 1);
        configTLP.Controls.Add(new ComboBox(), 2, configTLP.RowCount - 1);
        configTLP.Controls.Add(new TextBox(), 3, configTLP.RowCount - 1);
        configTLP.Controls.Add(new Button(), 4, configTLP.RowCount - 1);
    }

When clicked, only the first, third and fifth columns retain their size as per screenshot below.

enter image description here

TableLayoutPanel settings:

enter image description here


Solution

  • Adding { Dock = DockStyle.Fill} resolved the issue.

    private void addrowButton_Click(object sender, EventArgs e)
    {
        RowStyle temp = configTLP.RowStyles[configTLP.RowCount - 1];
        configTLP.RowCount++;
        configTLP.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height));
        configTLP.Controls.Add(new TextBox() { Dock = DockStyle.Fill}, 0, configTLP.RowCount - 1);
        configTLP.Controls.Add(new TextBox() { Dock = DockStyle.Fill}, 1, configTLP.RowCount - 1);
        configTLP.Controls.Add(new ComboBox() { Dock = DockStyle.Fill}, 2, configTLP.RowCount - 1);
        configTLP.Controls.Add(new TextBox() { Dock = DockStyle.Fill}, 3, configTLP.RowCount - 1);
        configTLP.Controls.Add(new Button() { Dock = DockStyle.Fill}, 4, configTLP.RowCount - 1);
    }