I am trying to use TFlowPanel component in the following manner:
- Place on the main form
Form1componentFlowPanel1: TFlowPanel. - Set
Form1.Width = 400,FlowPanel1.Align = alTop,FlowPanel1.AutoSize = True,FlowPanel1.AutoWrap = True. - Place on the
FlowPanel15 SpeedButtons and set theirWidthto 64. - Compile and run.
- Reduce width of the form (something about
Form1.Width = 200).
For some reason, the speedbuttons do not automatically line up in two rows when user resizes the form. Although, they do line up in two rows when AutoSize = False, AutoWrap = True.
What is the reason for this behavior and how to solve it?
Edit: I’ve found “quick and dirty” solution. The following code is the event handler to the TFlowPanel.OnResize event:
procedure TForm1.FlowPanel1Resize(Sender: TObject);
begin
with FlowPanel1 do
begin
AutoSize := False;
Realign; // line up controls
AutoSize := True; // adjust TFlowPanel.Height
end;
end;
However, I still wonder if there is a standard way to solve the problem.
I wasn’t able to find the exact reason of such behavior in code, but basically you’ve challenged two sizing properties to fight, the
AutoSizeandAlign. The problem is, I think, that when you resize a form, the control withAutoSizeconfigured to True andAlignset toalTopwill first try to autosize the control and then align to top of its parent. What I can tell for sure, these two properties shouldn’t be combined at least from their logical meaning.What I would suggest to your workaround is turn off the autosize by default and in
OnResizeevent turn it temporary on and back to off to automatically adjust the height. So in code it would change simply to: