我正在尝试使用TableLayoutPanel创建自动调整按钮网格。当我在运行时使用以下代码添加列时,它们的大小不均匀:
tableLayoutPanel.ColumnCount += 1;
for (var i = 0; i < tableLayoutPanel.RowCount; i++) {
var button = new Button {
Margin = Padding.Empty,
Padding = Padding.Empty,
Dock = DockStyle.Fill
}
tableLayoutPanel.Controls.Add(button);
}
包含1列的初始表格:
添加第二列:
添加第3列:
有没有办法让色谱柱均匀自动调整,而无需手动计算尺寸?
答案 0 :(得分:0)
有必要明确重置列样式:
private void RecalculateColumnStyles()
{
var cols = tableLayoutPanel.ColumnCount;
var pct = (float) 100 / cols;
tableLayoutPanel.SuspendLayout();
tableLayoutPanel.ColumnStyles.Clear();
for (var i = 0; i < cols; i++) {
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, pct));
}
tableLayoutPanel.ResumeLayout();
}