如果您只需要 Tiling,TableLayoutPanel
完全可以为您做到:
private void btnNewImage_Click(object sender, EventArgs e)
{
if (imgCount == 0)
tlp.Controls.Add(new Button { Text = "Image " + ++imgCount, Dock = DockStyle.Fill }, 0, 0);
else
{
// tlp is the TableLayoutPanel which is docked as Dock.Fill
if (tlp.RowCount == tlp.ColumnCount)
{
tlp.ColumnCount++;
for (int i = 0; i < tlp.RowCount; i++)
tlp.Controls.Add(new Button { Text = "Image " + ++imgCount, Dock = DockStyle.Fill }, tlp.ColumnCount - 1, i);
tlp.ColumnStyles.Clear();
for (int i = 0; i < tlp.ColumnCount; i++)
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)100.0 / (float)tlp.ColumnCount));
}
else
{
tlp.RowCount++;
for (int i = 0; i < tlp.ColumnCount; i++)
tlp.Controls.Add(new Button { Text = "Image " + ++imgCount, Dock = DockStyle.Fill }, i, tlp.RowCount - 1);
tlp.RowStyles.Clear();
for (int i = 0; i < tlp.RowCount; i++)
tlp.RowStyles.Add(new RowStyle(SizeType.Percent, (float)100.0 / (float)tlp.RowCount));
}
}
您可以new Button
用您的控件名称替换,我使用按钮作为示例控件。此代码假定通过点击添加新按钮,应将一行或一列添加到布局中。你可以自定义它来做任何你想做的事情。
希望这可以帮助。