Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi cannot remove children from VertScrollBox

I have a TVertScrollBox control with a TRectangle inside. When I click a button I take that rectangle and I copy it 20 times inside the scroll box.

// 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 the situation looks like this:

enter image description here


Problem

When I press another button I need to delete every rectangle in the scroll bot except the first one.

enter image description here

I am doing the reverse operation. In order to do this I have taken the code from an answer found in SO which states that I should run the loop backwards:

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

This code doesn't work because rectangles aren't deleted. Is that because I haven't set a Parent for the rectangle when adding it?

I've also tried something like RemoveObject(TRectangleVertScrollBox.Children[j])) but still nothing.

like image 501
Raffaele Rossi Avatar asked May 18 '18 11:05

Raffaele Rossi


1 Answers

VertScrollBox.AddObject method adds controls to the inner scroll box Content control. You have to iterate through Content children in order to remove added controls.

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

Object classes and particular object instances that are not added to the Content, but to scroll box itself are:

  • FContent
  • ResourceLink
  • TEffect instances
  • TAnimation instances
  • FVScrollInfo[0].Scroll
  • FVScrollInfo[1].Scroll
  • FHScrollInfo[0].Scroll
  • FHScrollInfo[1].Scroll
  • FSizeGrip
like image 128
Dalija Prasnikar Avatar answered Nov 04 '22 15:11

Dalija Prasnikar