In a windows form, I can add control dynamically by doing this:
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); }
How do I add controls dynamically in a FlowLayoutPanel
?
To create a FlowLayoutPanel control at design-time, you simply drag and drop a FlowLayoutPanel control from Toolbox to a Form in Visual Studio.
The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. You can wrap the control's contents from one row to the next, or from one column to the next. Alternately, you can clip instead of wrap its contents.
For a FlowLayoutPanel, you don't need to specify a .Location
since the controls are arranged for you:
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.
Just change "flowLayoutPanel1
" to the name of your FlowLayoutPanel
:
for (int i = 0; i < 5; i++) { Button button = new Button(); button.Tag = i; flowLayoutPanel1.Controls.Add(button); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With