Delphi Ensuring Left/alLeft or Top/alTop controls are positioned in the right order…
Posted by jpluimers on 2019/08/22
The post [WayBack] Why is my buttons not created in logical order? If I run this, my buttons area created ACB rather than ABC as I expected… – Johan Swart – Google+ reminded me the trouble of Delphi VCL and FMX have with alignment.
Basically you have to position your control away from your intended alignment position in order for it to work. So this fails:
procedure TForm1.FormCreate(Sender: TObject); var myToolBar: TToolBar; myCornerButton: TCornerButton; i: Integer; begin myToolBar := TToolbar.Create(Self); myToolBar.Parent := Self; for i := 0 to 2 do begin myCornerButton := TCornerButton.Create(tbarGrid); myCornerButton.Parent := myToolBar; myCornerButton.Align := TAlignLayout.Left; myCornerButton.Text := Chr(65 + I); end; end;
Basically you have to set myCornerButton.Left
to 1
before setting the Align
property.
Similar for the Top
property and TAlignLayout.Top
value.
The same holds for VCL Align
values alLeft
with setting the Left
property and alTop
with setting the Top
property before setting Align
.
See these for the actual properties and types:
- FMX:
- VCL:
See also this question: [WayBack] Delphi: How to programmatically adjust visual ordering of components with align = alTop
–jeroen
Stefaan Lesage said
Also … you should try to always set the .Parent as one of the last things you do. In your example the code might result in painting the 2 buttons about 3 times in that little loop. I know in this little example it won’t make much difference but if you are creating a lot of controls at runtime and setting a lot of visual properties it can make quite a bit of difference in painting performance.