首页 > 解决方案 > Delphi 无法从 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]))但仍然没有。

标签: delphifiremonkey

解决方案


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内容
  • 资源链接
  • TEffect 实例
  • TAnimation 实例
  • FVScrollInfo[0].Scroll
  • FVScrollInfo[1].Scroll
  • FHScrollInfo[0].Scroll
  • FHScrollInfo[1].Scroll
  • FSizeGrip

推荐阅读