好的,我正在尝试在运行时在 TScrollBox 表面上创建一些自定义数量的 TPanel,如下图所示。

为此,我正在使用以下代码,并且效果很好。
var
  pan: array of TPanel;
  maxp, i, x, y: Integer;
...
maxp := 10;
SetLength(pan, maxp);
for i := 1 to maxp do begin
  // x is correct value; doesn't cause problem
  // y is correct value; doesn't cause problem
  pan[i-1] := TPanel.Create(form1);
  with pan[i-1] do begin
    Width := 100;
    Height := 150;
    Top := x * 151;
    Left := y * 101;
    Parent := ScrollBox1;
    end;
  end;
现在,我无法将 TImage 对象放在每个具有相同索引的 TPanel 中(img[0] -> pan[0]、img[1] -> pan[1] 等)。看下图:

使用相同的逻辑,我尝试创建 TImage,但没有成功。
我正在使用此代码,但无法弄清楚出了什么问题。它对我来说看起来很简单,但不知何故它并没有提供预期的效果。
var
  pan: array of TPanel;
  img: array of TImage;
  maxp, i, x, y: Integer;
...
maxp := 10;
SetLength(pan, maxp);
SetLength(img, maxp);
for i := 1 to maxp do begin
  // x is correct value; doesn't cause problem
  // y is correct value; doesn't cause problem
  pan[i-1] := TPanel.Create(form1);
  with pan[i-1] do begin
    Width := 100;
    Height := 150;
    Top := x * 151;
    Left := y * 101;
    Parent := ScrollBox1;
    end;
  img[i-1] := TImage.Create(form1);
  with img[i-1] do begin
    Width := 98;
    Left := 1;
    Height := 148;
    Top := 1;
    // in original code next line had img[0]. which caused problem
    Picture.LoadFromFile('some_image_file');
    Parent := pan[i-1];
    end;
  end;
不知何故,它将所有 TImage 对象放在第一个 TPanel (pan[0]) 中的同一位置。这让我感到困惑,因为它说Parent := pan[i-1];但由于某种原因它总是将 TImage 放在 pan[0] 中。我尝试使用断点来查看每个 for 循环循环之后发生的情况(最后添加了 Application.ProcessMessages),它确实创建了 10 个不同的图像,但将它们放在 pan[0] 上。当然,最后它只显示加载到 pan[0] 中的最后一张图像。
我的问题是如何为每个动态 TPanel 制作一个动态 TImage(具有相同的数组索引)?
解决了!