Tuesday, March 1, 2011

CTreeCtrl - getting an item position

Is there a way of getting the position (index) of an item in a CTreeCtrl?
I am interested in the index of a node at its particular level.

I was thinking to maintain the item positions within the item "data" field, but the problem is that my tree is sorted and I cannot predict the position an item will receive (well, only if I sort the items in advance which I would like to avoid).

From stackoverflow
  • I don't think you can. I assumed that maybe the control could be treated as an array (maybe it still can but I can't find a reference).

    Anyways, there are no member functions (according to the MFC API) that give you access to that information

    Dana : Can you think of a way of bypassing this issue? I was thinking to maintain the item positions within the item "data" field, but the problem is that my tree is sorted and I cannot predict the position an item will receive.
  • Get the node handle and then iterate over the elem Iterate over all the elements, while you count the elements, till you reach the right item?

    Dana : I was hoping to do it a bit more efficiently than that. My tree can be quite large.
    Ray Hayes : If you're not using it already, put the index into the Tag field as you add items to the tree-structure (remembering to rebuild when you remove elements)

0 comments:

Post a Comment