Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the root index of a particular tree node?

I ran into a discrepancy with indexes of nodes in a tree view. This tree has only 2 levels of hierarchy, a number of root nodes and a number of nodes under each of those. When I read TTreeView.Selected.Index, I get the index of the root node only, but when I read TTreeView.Items[TTreeView.Selected.Index] (iterating in a loop), I get a completely different tree node.

A more specific example, suppose I have this data:

  • Root 1
    • Item 1.1
    • Item 1.2
    • Item 1.3
  • Root 2
    • Item 2.1
    • Item 2.2
    • Item 2.3
  • Root 3
    • Item 3.1
    • Item 3.2
    • Item 3.3

Now suppose I select "Root 3" and read TTreeView.Selected.Index, it will return 2. However, when I read TTreeView.Items[2] it returns "Item 1.2" because it's really the third item in the list. The one I have selected "Root 3" is index number 8 in reality.

What would be the correct way to read the index of the currently selected root node so I get 8 instead of 2?

like image 847
Jerry Dodge Avatar asked Jan 14 '23 14:01

Jerry Dodge


1 Answers

TTreeNode.Index is relative to TTreeNode.Parent. TTreeView.Items[] uses absolute indexes, so use TTreeNode.AbsoluteIndex instead. However, Items[Selected.AbsoluteIndex] is redundant and inefficient, as it returns the same TTreeNode that Selected returns.

like image 149
Remy Lebeau Avatar answered Jan 22 '23 01:01

Remy Lebeau