【发布时间】:2015-03-11 06:37:42
【问题描述】:
我有一个表格,里面有一个 TableLayoutPanel,它有 1 行和 1 列。我用控件动态地填充这个 TableLayoutPanel。
首先我创建表:
private void generateTable(int columnCount, int rowCount)
{
//Clear out the existing controls, we are generating a new table layout
tblLayout.Controls.Clear();
//Clear out the existing row and column styles
tblLayout.ColumnStyles.Clear();
tblLayout.RowStyles.Clear();
//Now we will generate the table, setting up the row and column counts first
tblLayout.ColumnCount = columnCount;
tblLayout.RowCount = rowCount;
for (int x = 0; x < columnCount; x++)
{
//First add a column
tblLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
for (int y = 0; y < rowCount; y++)
{
//Next, add a row. Only do this when once, when creating the first column
if (x == 0)
{
tblLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
}
}
}
然后在其单元格中添加控件:
tblLayout.Controls.Add(my_control, column, row);
我还创建了这个函数来根据 TableLayoutPanel 的行数和列数调整表单的大小。
private void forceSizeLocationRecalc()
{
this.Height = 0;
this.Width = 0;
int col = this.tblLayout.ColumnCount;
int row = this.tblLayout.RowCount;
for (int i = 0; i < row; i++)
{
this.Height += this.tblLayout.GetControlFromPosition(0, i).Height;
}
for (int i = 0; i < col; i++)
{
this.Width += this.tblLayout.GetControlFromPosition(i, 0).Width;
}
}
并在表单设置结束时调用它。
问题是,例如,如果我首先有一个 tableLayout (col=2, row=3),并且 i 传递给表格布局为 (col = 3, row = 1) 维度的情况表单的大小与以前的相同,因此我必须手动调整表单的大小。我希望我的表单根据列数和行数自动调整大小。
有什么想法吗?
谢谢
【问题讨论】:
标签: c# winforms tablelayout