TableLayoutPanel 新行列不遵循设置大小

TableLayoutPanel new row columns not following set size

我有一个包含 5 列的 TableLayoutPanel。

列的宽度已设置为固定值以匹配其内容。

我有一个添加行按钮,代码如下:

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

点击后,只有第一、第三和第五列的大小保持不变,如下面的屏幕截图所示。

TableLayoutPanel 设置:

添加 { Dock = DockStyle.Fill} 解决了问题。

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