0

I populate ScrollBoxin the alike way:

procedure TForm1.FormCreate(Sender: TObject);
var
  i: word;
begin
  for i := 1 to 3 do
  begin
    with TLabel.Create(ScrollBox1) do
    begin
      Parent := ScrollBox1;
      Top := 1000;
      AutoSize := False;
      Align := alTop;
      Height := 25;
      Caption := 'Label' + IntToStr(i);
    end;
  end;
    ScrollBox1.Realign;
end;

When the code is run under Delphi I get the follwong result:

enter image description here

The order of items is proper. But when I call the same code under Lazarus I get:

enter image description here

The order of items is reverse. I can solve the issue by reverse creation of ScrollBox children and/or adding {IFDEF ...} but I suspect this is not reliable. Adding compiler switches will double the volume of code making it bulky and difficult to read.

Is there a way to do unified reliable Delphi-Lazarus code for this purpose?

APPENDED

explanation on comment of @TomBrunberg

If I create chidren in reverse order (for instance for i := 3 downto 1) I get the opposite result: Delphi produces reverse and Lazarus - direct order. That is why I was saying about doubling of code.

APPENDED 2

on note of Tom Brunberg

When the same code is called from a Button onClick event handler the code behaviour becomes opposite (and again different in Lazarus and in Delphi).

APPENDED 3

Can I trust for i := 1 to 3... Top := 1000 + i; as it gives the expected result?

asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • Without possibility to test, maybe if you reverse the order (3 downto 1) and set `Top := 0;` or -1, leaving everything else as it is. – Tom Brunberg Nov 22 '17 at 08:22
  • @TomBrunberg Nope(( In this case we get opposite results: Delphi produces reverse and Lazarus – direct order. This is I was supposing under 'reverse creation of `ScrollBox` children' in my Q. However I tried to assign `Top := 0` and `-1` with the above mentioned result – asd-tm Nov 22 '17 at 08:56
  • Try again. When I reverse the loop (3 downto 1) and set Top to 0 (or -1) I get the correct order of the labels in Delphi XE7, when executed from a button click event. – Tom Brunberg Nov 22 '17 at 09:07
  • Ok, I tried also in a form create event, and then the order is wrong in Delphi. – Tom Brunberg Nov 22 '17 at 09:10
  • @TomBrunberg Thank you for being interested in my Q. Yes. That is what I supposed under 'reliable code'. I can not be sure in the behavior of this simple chidren creation template. – asd-tm Nov 22 '17 at 09:25
  • So, you have concluded the answer is no. – Tom Brunberg Nov 22 '17 at 09:53
  • @TomBrunberg Have I? I want to believe that such a typical process as population of a `TScrollBox` instance can be done in a safe and reliable way and have not yet lost the hope)). – asd-tm Nov 22 '17 at 11:23
  • 1
    @TomBrunberg I have found one way `for i := 1 to 3`...`Top := 1000 + i;` But now I am not sure if I can trust it because of the following reason. I suppose that after assigning `Align := alTop;` `Top` is changed and this happens every iteration before the next child is created. So every single iteration `Top` of the previous child should? be less then `Top` of the next. – asd-tm Nov 22 '17 at 11:33
  • Tracing into the source will tell you exactly! No? – Tom Brunberg Nov 22 '17 at 11:40
  • I think the problem has to do with the combination of Align and Top properties. I had similar problems a while ago. When I create controls by code, like this, I tend to not rely heavily on the Align property. You should try incrementing the Top property in the loop. If you use 25 Height, then increment it by 25 for each label. I'm using mostly FireMonkey now... but there are similar problems there too. When you have a bunch of controls with the same coordinates and alignment and you start hiding and showing them, they will reappear in a mostly random order. – Frazz Nov 24 '17 at 10:16

0 Answers0