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?
TTreeNode.Indexis relative toTTreeNode.Parent.TTreeView.Items[]uses absolute indexes, so useTTreeNode.AbsoluteIndexinstead. However,Items[Selected.AbsoluteIndex]is redundant and inefficient, as it returns the sameTTreeNodethatSelectedreturns.