Creating Controls in a TScrollBox at Runtime Starts to ReOrder

375 Views Asked by At

I'm using Delphi 10.1 and Firemonkey and looking to add controls to a TScrollBox at Runtime and have come across some strange behaviors.

For this example I create Labels and add them to a TScroll box.

The Label is set as TAlignLayout.Top so I assume every new Label created will sit below the previous.

  • First Label that gets created goes to the top.
  • Second Label that gets created goes below the first.
  • Third Label that gets created goes between the first and second Label.
  • Any more Labels that created then stacks under the First Label.

enter image description here

I want the Labels to be created from top down in order of creation. Any ideas what I've done wrong?

This is the code to create the Labels:-

procedure TForm1.Button4Click(Sender: TObject);
var
  lbFileDate: TLabel;
begin
  ScrollBox2.BeginUpdate;
  lbFileDate := TLabel.Create(ScrollBox2);
  lbFileDate.Parent := ScrollBox2;
  lbFileDate.Align := TAlignLayout.Top;
  lbFileDate.Text := DateTimeToStr(Now);
  ScrollBox2.EndUpdate;
end;

I've done something very similar in Delphi 10.1 VCL and the creation process works by always putting the last Label to the top.

tia

1

There are 1 best solutions below

1
On BEST ANSWER

Looks like for already laid out controls any additional controls go to where they can be squeezed in based on their coordinates which default to 0,0. You can give a new control a large y so it starts under the rest. Before they are laid out like inside a begin/end update block they can be placed on top of each other and will get laid out based on creation order.

procedure TForm1.Button1Click(Sender: TObject);
var
  lbFileDate: TLabel;
begin
  ScrollBox2.BeginUpdate;
  for var I : integer := 1 to 10 do
  begin
    lbFileDate := TLabel.Create(ScrollBox2);
    lbFileDate.Parent := ScrollBox2;
    lbFileDate.Position.Y := 1E10;
    lbFileDate.Align := TAlignLayout.Top;
    lbFileDate.Text := DateTimeToStr(Now) + ' ' + IntToStr(I);
  end;
  ScrollBox2.EndUpdate;
end;