Search code examples
c#flowlayoutpanel

Why flowlayoutPanel is extending horizontally?


I have set this flowLayoutPanel, the controls inside arrange well, till the last arrives to the bottom border of the panel, then the controls start arranging on the right side (forming another column) keepping the vertical flow. I just want one column.

this.panel.Anchor = 
((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)| System.Windows.Forms.AnchorStyles.Right)));
this.panel.AutoScroll = true;
this.panel.BorderStyle = BorderStyle.None;          
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.panel.Location = new System.Drawing.Point(0, 184);
this.panel.Name = "myPanel";
this.panel.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.panel.Size = new System.Drawing.Size(300, 371);
this.panel.TabIndex = 9;

Solution

  • Use

    this.panel.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;

    instead of

    this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;

    if you want only one column than please add below code to your application just after control added to your flowlayoutpanel

    this.panel.SetFlowBreak(<<YOUR_ADDED_CONTROL_NAME>>, true);

    Example

    Button btn1 = new Button();
    btn1.Text = "TEST";
    btn1.Height = 30;
    btn1.Width = 100;
    
    this.panel.Controls.Add(btn1);
    this.panel.SetFlowBreak(btn1, true);