Search code examples
delphidelphi-7virtualtreeviewtvirtualstringtree

Virtual TreeView Loop


I would like to go through ALL the roots of a VirtualTreeView and delete them.

I don't want to clear it.

I get an access violation with this code:

var
 Node : PVirtualNode;
begin
 if VirtualStringTree1.GetFirst = NIL then exit;
 Node := NIL;
 repeat
  if Node = NIL then 
   Node := VirtualStringTree1.GetLast 
  else Node:=VirtualStringTree1.GetPrevious (Node);
  if Node <> NIL then VirtualStringTree1.DeleteNode(Node);
 until Node = VirtualStringTree1.GetFirst;
end;

Thank you for your help.


Solution

  • You have a logical error in your implementation: after deleting the node your local variable Node points to a non-existing node.

    I don't understand why you don't want to just clear the tree, but you could delete all nodes from last to first like this:

    var
      Node, TmpNode: PVirtualNode;
    begin
      Node := Tree.GetLast;
      while Assigned(Node) do
      begin
        TmpNode := Tree.GetPrevious(Node);
        Tree.DeleteNode(Node);
        Node := TmpNode;
      end;
    end;