Delphi 无法从 VertScrollBox 中删除子项

Delphi cannot remove children from VertScrollBox

我有一个 TVertScrollBox 控件,里面有一个 TRectangle。当我单击一个按钮时,我会选择那个矩形并将其复制到滚动框中 20 次。

// var Rectangle: TRectangle;
VertScrollBox.BeginUpdate;
for i := 0 to 19 do
  begin

    //Copy the rectangle that is already inside the ScrollBox
    Rectangle:= TRectangle(RectangleTemplate.Clone(VertScrollBox));
    VertScrollBox.AddObject(Rectangle);

  end;
VertScrollBox.EndUpdate;

所以情况是这样的:


问题

当我按下另一个按钮时,我需要删除滚动机器人中的每个矩形 除了 第一个。

我正在做反向操作。为了做到这一点,我从 SO 中找到的答案中获取了代码,其中指出我应该 运行 向后循环:

for j := VertScrollBox.ChildrenCount-1 downto 1 do
  if (VertScrollBox.Children[j] is TRectangle) then
    VertScrollBox.RemoveObject(VertScrollBox.Children[j]);

此代码无效,因为未删除矩形。是不是我添加的时候没有给矩形设置Parent

我也试过 RemoveObject(TRectangleVertScrollBox.Children[j])) 但还是不行。

VertScrollBox.AddObject 方法将控件添加到内部滚动框 Content 控件。您必须遍历 Content 个子项才能删除添加的控件。

for j := VertScrollBox.Content.ChildrenCount-1 downto 1 do
  if (VertScrollBox.Content.Children[j] is TRectangle) then
    VertScrollBox.Content.RemoveObject(VertScrollBox.Content.Children[j]);

对象 类 和未添加到 Content 但滚动框本身的特定对象实例是:

  • F 内容
  • 资源链接
  • T效果实例
  • T动画实例
  • FVScrollInfo[0].Scroll
  • FVScrollInfo[1].Scroll
  • FHScrollInfo[0].Scroll
  • FHScrollInfo[1].Scroll
  • FSizeGrip

VertScrollBox.Content.DeleteChildren