Add controls dynamically in flowlayoutpanel

70,978

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);
}
Share:
70,978
Karlx Swanovski
Author by

Karlx Swanovski

Updated on July 09, 2022

Comments

  • Karlx Swanovski
    Karlx Swanovski almost 2 years

    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?

  • Master Yoda
    Master Yoda over 9 years
    What on earth does SQL have to do with this question?
  • Mark
    Mark almost 9 years
    Helped me as I am adding controls to a form from a database