Home > Net >  TVirtualStringTree adding certain sequence code to nodes text
TVirtualStringTree adding certain sequence code to nodes text

Time:02-03

I have TVirtualStringTree and while am trying to modify its text to be as the next (Code Text) :

1 Node1
  11 Node2
   111 Node3
   112 Node4
    1121 Node5

I have created this function which creates Test node for this purpose :

function GetNodeCode(Tree: TVirtualStringTree; SelectedNode: PVirtualNode): String;
var
  RootNod, TestNode: PVirtualNode;
  Code: String;
  i, count: integer;
begin
  RootNod := SelectedNode;
  Result := '';
  Count := 0;
  if Assigned(RootNod) then
  begin
    TestNode := Tree.AddChild(RootNod);
    RootNod := TestNode;
    while Assigned(RootNod) do
    begin
      if RootNod.Index = 0 then
        Result := '1'   Result
      else
        Result := IntToStr(RootNod.Index   1)   Result;
      RootNod := RootNod.Parent;
    end;
    Tree.DeleteNode(TestNode);
  end
  else
  begin
    RootNod := Tree.GetFirst;
    while Assigned(RootNod) do
    begin
      if not Assigned(RootNod.Parent) then
        Count := Count   1;
      Result := IntToStr(Count   1);
      RootNod := Tree.GetNext(RootNod);
    end;
  end;
end;

But when running the code its somehow giving Access violation (after the third count in the loop) in this line :

if RootNod.Index = 0 then

I have noted that the RootNod is assigned but all its data shows inaccessible value ... can anybody explain what's the wrong with this code .

CodePudding user response:

Try to use Tree.NodeParent[RootNod]; instead of RootNod.Parent. For the root node of the tree, its Parent is the tree itself, so it is "Assigned", but it is not PVirtualNode

  • Related