user1175743
user1175743

Reputation:

Getting Selected TTreeview Children

I am trying to get all child nodes of the selected node in a treeview but have run into a few problems.

Take this treeview as an example:

enter image description here

I want to get all nodes that are child to yellow highlighted "Folder" node, which would be the child nodes with a blue line next to it.

This is what I tried:

procedure Form1.GetTreeChilds(ANode: TTreenode);
begin
  while ANode <> nil do
  begin
    ListBox1.Items.Add(ANode.Text);
    ANode := ANode.GetNext;
  end;
end;

It works except that it also returns Item 6 which is not child to the yellow highlighted "Folder".

What do I need to change or do differently to only get the child nodes to the yellow highligted Folder?

Thanks.

Upvotes: 1

Views: 3763

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595961

Try this instead:

procedure Form1.GetTreeChilds(ANode: TTreeNode);
begin
  ANode := ANode.GetFirstChild;
  if ANode = nil then Exit;
  ListBox1.Items.BeginUpdate;
  try
    repeat
      ListBox1.Items.Add(ANode.Text);
      GetTreeChilds(ANode);
      ANode := ANode.GetNextSibling;
    until ANode = nil;
  finally
    ListBox1.Items.EndUpdate;
  end;
end;

Upvotes: 4

Related Questions