在 Windows 窗体中,我可以通过这样做动态添加控件:
for (int i = 0; i < 5; i++)
{
Button button = new Button();
button.Location = new Point(160, 30 * i + 10);
button.Tag = i;
this.Controls.Add(button);
}
如何在 FlowLayoutPanel
中动态添加控件?
最佳答案
对于 FlowLayoutPanel ,您无需指定 .Location
,因为控件已为您安排好:
Represents a panel that dynamically lays out its contents horizontally or vertically. ... The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. Its contents can be wrapped from one row to the next, or from one column to the next.
只需将“flowLayoutPanel1
”更改为您的FlowLayoutPanel
的名称:
for (int i = 0; i < 5; i++)
{
Button button = new Button();
button.Tag = i;
flowLayoutPanel1.Controls.Add(button);
}
关于c# - 在flowlayoutpanel中动态添加控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16900522/