Recent

Author Topic: TTreeView programatically reaching subitems based on parents [SOLVED]  (Read 463 times)

vargatam77

  • New Member
  • *
  • Posts: 11
Hey again. I thought TTreeView will be a good object for this but obviously it is not... I need to create a multilevel visual list, just like ttreeview, except I would need to access nth parent's nth child's nth child, which in a world of a multilevel TListItems would be friggin easy, except TListView has a FLAT itemlist so TListView.Item[2].Items[1].Items[0] is not the 3rd parents sendond child's first child. ohh how easy it would be. Any out of the box solution for this without threading thorough the tree manually?
Cheers
« Last Edit: September 09, 2025, 05:40:52 pm by vargatam77 »

Khrys

  • Sr. Member
  • ****
  • Posts: 348
Re: TTreeView programatically reaching subitems based on parents
« Reply #1 on: September 09, 2025, 04:32:11 pm »
You can access child nodes in a non-flattened way using TTreeNodes.TopLvlItems.
TTreeView.Items.TopLvlItems[2].Items.TopLvlItems[1].Items.TopLvlItems[0]  does what you expect, although it's pretty verbose.

vargatam77

  • New Member
  • *
  • Posts: 11
Re: TTreeView programatically reaching subitems based on parents
« Reply #2 on: September 09, 2025, 05:40:22 pm »
Ahh, well, as long as it works.. its perfect, thanks. I have even seen toplvlitems but i though it is about top level. silly me :D of course on every level the toplevel is that level :D thanks again

wp

  • Hero Member
  • *****
  • Posts: 13219
Re: TTreeView programatically reaching subitems based on parents [SOLVED]
« Reply #3 on: September 09, 2025, 06:23:20 pm »
If I understand the question correctly I'd implement NthChild and NthParent as node methods using a class helper:

Code: Pascal  [Select][+][-]
  1. type
  2.   TTreeNodeHelper = class helper for TTreeNode
  3.   public
  4.     function NthParent(N: Integer): TTreeNode;
  5.     function NthChild(N: Integer): TTreeNode;
  6.     function NthSibling(N: Integer): TTreeNode;
  7.   end;
  8.  
  9. function TTreeNodeHelper.NthChild(N: Integer): TTreeNode;
  10. var
  11.   i: Integer;
  12. begin
  13.   Result := Self;
  14.   if Result <> nil then
  15.     for i := 1 to N do
  16.       if Result.HasChildren then
  17.         Result := Result.GetFirstChild
  18.       else
  19.       begin
  20.         Result := nil;
  21.         exit;
  22.       end;
  23. end;
  24.  
  25. function TTreeNodeHelper.NthParent(N: Integer): TTreeNode;
  26. var
  27.   i: Integer;
  28. begin
  29.   Result := Self;
  30.   if Result <> nil then
  31.     for i := 1 to N do
  32.       if Result.Parent <> nil then
  33.         Result := Result.Parent
  34.       else
  35.       begin
  36.         Result := nil;
  37.         exit;
  38.       end;
  39. end;
  40.  
  41. function NthSibling(N: Integer): TTreeNode;
  42. var
  43.   i: Integer;
  44. begin
  45.   Result := Self;
  46.   if result <> nil then
  47.     for i := 1 to N do
  48.       if Result.GetNextSibling <> nil then
  49.         Result := Result.GetNextSibling
  50.       else
  51.       begin
  52.         Result := nil;
  53.         exit;
  54.       end;
  55. end;

Then you can find the 3rd parent's 2nd child's 1st child by successive calls:
Code: Pascal  [Select][+][-]
  1. function TForm1.ButtonClick(Sender: TObject);
  2. begin
  3.   TreeView1.Selected := TreeView1.Selected.NthParent(3).NthChild(2).NthChild(1);
  4. end;
This combination, however, is rather trivial because going towards the root by 3 generations then back by 2 and then 1 generation, always via the first sibling, ends at the starting node... Maybe I am misunderstanding something. Therefore I also added a NthSibling method.

See a sample project in the attachment
« Last Edit: September 09, 2025, 06:29:28 pm by wp »

vargatam77

  • New Member
  • *
  • Posts: 11
Re: TTreeView programatically reaching subitems based on parents [SOLVED]
« Reply #4 on: September 10, 2025, 02:54:14 pm »
If I understand the question correctly I'd implement NthChild and NthParent as node methods using a class helper:

See a sample project in the attachment

Thanks but toplvlitems have solved the issue. I simply needed to be able to access elements of a level by their index within their own respective level. for example, i query alsa for audio devices available, i save them, i will only know their indexes on the level of cards. then i iterate through their profiles, again i will only know which card has which profiles etc. to find one i need to be able to see each level separately like 2nd card's 3rd profile. this works well with toplvlitems, so no need for any further manual implementation... thanks anyway, i still appreciate trying to help me.

 

TinyPortal © 2005-2018