views:

346

answers:

3

is there anyway to get the index of selected tree view node or do they even have one?

A: 

Something like this:

Selected=(HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE1,
          TVM_GETNEXTITEM,TVGN_CARET,(LPARAM)Selected);
if(Selected==NULL)
{
  MessageBox(hWnd,"No Items in TreeView","Error",
             MB_OK|MB_ICONINFORMATION);
  break;
}

Comes from here

Tony
'Selected' here is handle to treeview item. in my case, I want an integer value.
Dave18
A: 

There's no such thing, because such an index can be defined in many ways.

If you collapse and expand the nodes, is the selected index going to change, or stay constant?

To get this functionality, you'll have to roll your own algorithm with whatever set of rules you want. Or, simply go by the selected node as shown (this is much more common).

Jon Seigel
Root and its parent nodes would their own order of index. I could use tag property to give each node an index but deleting a node from the middle would change the order of index of the nodes behind it.
Dave18
I just need this functionality to easily find an element from STL container which is maintaining tree view data.
Dave18
If the container is giving you index values, you'll have to roll your own algorithm to match. If it's giving you node handles, then you'll need to take a slightly different approach.
Jon Seigel
+1  A: 

Since you're asking for "index" only to be able to find data associated with this item you should know that tree control can hold your data. Every item (TVITEM struct) has a lParam member that you can use for this.

If you really need a container do as avakar suggested. Use HTREEITEM as key.

Nikola Smiljanić