Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 401
Ben Avatar asked Dec 06 '25 18:12

Ben


1 Answers

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;
like image 60
Ondrej Kelle Avatar answered Dec 08 '25 20:12

Ondrej Kelle