FlowLayoutPanel and AutoSize

A FlowLayoutPanel places its children one after another and starts a new row when the width runs out. It is the container for a row of buttons, a strip of tags, a gallery of thumbnails: anything whose number of children is not known when the form is designed and whose order is all that matters.

A panel that places its children

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { public Form1() { Text = "Commands"; ClientSize = new Size(420, 160); var flow = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 90, FlowDirection = FlowDirection.LeftToRight, WrapContents = true, AutoScroll = true, Padding = new Padding(8), }; foreach (var caption in new[] { "New", "Open", "Save", "Save as", "Export", "Print", "Close" }) { flow.Controls.Add(new Button { Text = caption, AutoSize = true, Margin = new Padding(4) }); } Controls.Add(flow); } }

dotnet build app.csproj

Build succeeded.
    0 Warning(s)
    0 Error(s)

The listing is built and not run. Each button sizes itself to its caption, because AutoSize = true on a button is what makes it as wide as its text needs, and the four-pixel Margin is the gap the flow panel leaves between neighbours. Seven buttons of different widths flow into as many rows as the panel needs, and the panel scrolls when even the rows do not fit.

The order of Controls is the flow order, so adding a button at the front puts it at the start of the row: flow.Controls.Add(button) appends, flow.Controls.SetChildIndex(button, 0) moves it to the front. A flow panel cannot align its children to the right edge of the row; a TableLayoutPanel with an empty first column does that.

Direction and wrapping

The four directions are worth one table, because the pair that looks symmetric is not:

The margin of a child is the flow panel's spacing, and it is Margin on the child rather than Padding on the panel: a flow layout reads each child's margin and adds it around that child. A uniform gap is one assignment per child, or a loop over the children after they are added.

AutoSize and AutoSizeMode

Two AutoSize properties are in play, and they are different ones:

A flow panel that grows needs a parent that lets it: inside a docked Fill area it is given the whole space and AutoSize does nothing, because the layout is in charge. It matters when the panel sits in a TableLayoutPanel row of AutoSize, or on a form without a dock.

The wrapping arithmetic that runs

Wrapping is a running position and a comparison, and it runs here. Six items of different widths flow into a panel 400 pixels wide:

using System; using System.Collections.Generic; var items = new List<(int Width, int Height)> { (120, 40), (90, 40), (140, 40), (60, 40), (110, 40), (80, 40), }; var panelWidth = 400; var x = 0; var y = 0; var rowHeight = 0; foreach (var item in items) { if (x > 0 && x + item.Width > panelWidth) { x = 0; y += rowHeight; rowHeight = 0; } Console.WriteLine($"item at x={x,3} y={y,3} w={item.Width}"); x += item.Width; rowHeight = Math.Max(rowHeight, item.Height); } Console.WriteLine($"rows used: {(y / 40) + 1}");

dotnet run

item at x=  0 y=  0 w=120
item at x=120 y=  0 w=90
item at x=210 y=  0 w=140
item at x=  0 y= 40 w=60
item at x= 60 y= 40 w=110
item at x=170 y= 40 w=80
rows used: 2

The output is the real run. Three items fit in the first row because 120 + 90 + 140 is 350 and the fourth would end at 410; the row height is taken from the tallest item of the row, which is why the second row starts at 40. The guard x > 0 is the case where a single item is wider than the panel: without it the loop would wrap before every item and leave an empty first row. The last line counts the rows the same way a panel's height does.

Exercises

  1. Add a seventh item of 200 pixels and run the program. Where does it land?
  2. Make the row height the sum of the heights instead of the maximum, and run it again.
  3. Explain from the documentation the difference between AutoSizeMode.GrowOnly and GrowAndShrink on a panel.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the default FlowDirection?

    Answer

    LeftToRight

    A row that fills from the left and wraps downward.

  2. What does WrapContents = false do?

    Answer

    Keeps every child on one line and lets it overflow

    It turns a flowing panel into a single fixed row.

  3. What is the spacing between the children of a flow panel?

    Answer

    Each child's Margin

    The flow layout reads Margin on each child; Padding belongs to the panel's own edge.

  4. What does AutoSize on a Button do?

    Answer

    Makes the button as wide as its caption needs

    It is off by default on a button and on by default on a label.

  5. Which AutoSizeMode lets a panel shrink when a child is removed?

    Answer

    GrowAndShrink

    GrowOnly is the default and keeps the largest size the panel had.

  6. What does a flow panel do with a child wider than itself?

    Answer

    Gives it its own row, which overflows or scrolls

    The wrap happens before the child and the child keeps its width.

  7. Where did the run place the fourth item?

    Answer

    x=0 y=40

    Three items filled 350 pixels and the fourth started the second row.

  8. How is the height of a row decided?

    Answer

    The tallest item in the row

    The run took Math.Max of the heights of the row.

  9. Why does the wrapping loop need the x > 0 guard?

    Answer

    So an item wider than the panel starts a row instead of wrapping before every item

    Without it the condition is true at x = 0 for a wide item.

  10. When is a TableLayoutPanel better than a FlowLayoutPanel?

    Answer

    When the cells must line up in a grid

    A flow order is not a grid: columns of a table stay aligned.

Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows. The logic that draws nothing was also run headless on the same machine, and its output is the run output printed below each of those programs.

Article author: Arthur Isaev

Banner image