Search code examples
.netwinformslayoutflowlayoutpanel

Removing vertical space between controls in a FlowLayoutPanel


I have a bunch of buttons of different sizes in a FlowLayoutPanel. I removed the margins but there seems to be a default grid-style layout. How can I make it so that the buttons all stick together?

enter image description here

I would like to remove the space between "Algebra" and the button above, sticking them as close together as possible.


Solution

  • By default, a FlowLayoutPanel lays outs its controls in rows from left to right, then top to bottom. In your example, the buttons have the same width but different heights, so a gap appears. To remove the gap, you can tell the FlowLayoutPanel to lay out its controls in columns from top to bottom, then left to right:

    1. Change the FlowDirection property of the FlowLayoutPanel from LeftToRight to TopDown.
    2. Swap the order of the "gravity" button and the unlabeled button to restore the layout in your picture.
    3. If the unlabeled button winds up beneath the "gravity" button, set the FlowBreak property of the "gravity" button to True so that the unlabeled button is placed in a new column.