0

I am adding to my main Form a UserControl with Dock Property set to Fill. The UserControl has a FlowLayoutPanel that is also docked (Fill) and the AutoScroll property is set to True.

The FlowLayoutPanel contains 5 groupboxs, each on has its own size. When the Form (or UserControl) is in his normal size (716x520), the groupboxs go from top to bottom, all good.

But when I maximize the Form (or just make it bigger), the groupboxs get re-arranged, and a strange space appears after the 2 first groupboxs. It's like there are rows and if another groupbox from the same row is bigger, then a space will appear to complete the height difference.

Here are some images to explain it better : Normal size of the UserControl When the UserControl is maximized

Haytam
  • 4,643
  • 2
  • 20
  • 43
  • When you maximize your form your panel gets bigger as well. So FLP recalculates layout and this is what it came up with. It does not try to solve the infamous bin packing problem, that's an NP-hard problem. It needs to be done in less time than the blink of an eye. Feature, not a bug. – Hans Passant Aug 27 '16 at 13:29
  • So is there a solution to this or I should just accept this? – Haytam Aug 27 '16 at 13:50

1 Answers1

0

Obviously you specify LeftToRight for the FlowDirection instead of TopDown. So when that layout panel is wide enough, there will be room for other group boxes in the first row...

That layout is somewhat similar to word wrapping for text where you get as many words as possible of the first line depending on the available width.

So if you change the direction, you will have a single column if you have enough height.

There are other possible solutions for that problem. Assuming that group boxes are all fixed sizes, then you might set the flow layout panel docking to None so that the panel won't be resized to the available width (and then prevent having multiple columns by giving an appropriate size to that panel.

Another possibility would be to use a layout based on a table. Given that all your group boxes have the same width, this is not necessary (it is a bit simpler to use flow layout when it works).

In practice however, you might prefer to show all group boxes on screen if there is enough room and avoid a scroll bar. This can be done using flow layout. In your case, you might want to use TopDown flow, top docking, layout panel auto sizing (and probably put the auto scroll on the user control).

Phil1970
  • 2,605
  • 2
  • 14
  • 15
  • Well, I just made some tests. If you want `TopDown` layout, then the layout panel should be docked left but you get an horizontal scroll bar if not all group boxes can fit. You have Otherwise, `LeftToRight` and `Top` docking would give you the same problem you have initially... Thus `AutoSize` and `FlowDirection` are related and you cannot get exactly what you probably want without custom code. – Phil1970 Aug 27 '16 at 14:24
  • I tried TopDown FlowDirection but when the Form is maximized and the size of the UserControl (and The FLP) is increased, enough to have 2 groupboxs at the same line, it only shows 1 groupbox per line and it goes all the way down. Is there a way to show them Top to Bottom when it's 716x520 but when it's big enough, show 2 groupboxs at a line but without those spaces ? – Haytam Aug 27 '16 at 15:55
  • If the layout is `TopDown`, then the height of the container is fixed and the width is adjusted. You won't have any gap because first column is fill from the top, then second one, etc. But as the varying dimension is X, the scrollbar would be horizontal if there is not enough room. If you want to fill line first, then all group boxes should have same height to avoid those space. Put number as the caption of your group boxes and it will be easy to figure out hot flow layout works. If after trying options I told, you cannot get what you want, then the only solution is to do your own layout. – Phil1970 Aug 27 '16 at 18:00