Search code examples
delphidelphi-xe6

Delphi - creating dynamic elements alignTop order


I got a ScrollBox, and creating few GroupBoxes inside dynamically with alTop property. When I create them first time after application run, the first created is top most. And that's fine. But when I free them all and create again in same order, top most is the last one. And every time when doing that again, it will be last one until application restart. Only after restart, it creates it in right order... Why is that? Any idea on how to fix that?


Solution

  • When you create controls dynamically using the Align property, you need to take care over their positioning. Assuming you use alTop, follow this strategy:

    • For the first control set the Top to 0.
    • For the second control set the Top to be the bottom of the first control.
    • In general, each control's Top is to be set to the bottom of the previous control.

    This question appears to be related: How to dynamically create controls aligned to the top but after other aligned controls?