Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add controls dynamically in flowlayoutpanel

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?

like image 394
Karlx Swanovski Avatar asked Jun 03 '13 15:06

Karlx Swanovski


People also ask

How do I add controls to 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.

What is a Flow layout panel?

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.


1 Answers

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); } 
like image 199
Idle_Mind Avatar answered Sep 24 '22 14:09

Idle_Mind