Link to home
Start Free TrialLog in
Avatar of sun307
sun307

asked on

Getting the index of CTreeCtrl item

Hi !
I want to get the index of the selected node in my CTreeCtrl. When i do a getselecteditem() then it returns a HTREEITEM type. where as i wants the index number of that particular node at that level.
How can i do that.
Thanx in advance
Regards,
Sun307
Avatar of Vinayak Kumbar
Vinayak Kumbar

Hi,

U can do all the CTreeCtrl operations using the HTREEITEM, u can delete, select..., then why u need the item index?

Try it out.
VinExpert
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

One more method, to get index. Store the specific index value while inserting itself using lParam parameter of TV_ITEM, as
TV_INSERTSTRUCT TreeCtrlItem;
TreeCtrlItem.item.lParam = 0;
and set other parameters and insert it,
Then while inserting childs give specific values. To retrive that,

HTREEITEM hTreeItem = m_Property.GetSelectedItem();
      
TV_ITEM TreeCtrlItem;
TreeCtrlItem.mask = TVIF_HANDLE | TVIF_PARAM;
TreeCtrlItem.hItem = hTreeItem;

m_Property.GetItem(&TreeCtrlItem);
Now TreeCtrlItem.lParam will give u the value which is specified by U. Here m_Property is the control variable for CTreeCtrl.

For datails U can look into the code of vc++ sample FIRE.

Hope this helps.
VinExpert

> One more method, to get index.

That doesn't store (or retrieve) an index.  It stores or retrieves an arbitrary value.

..B ekiM
Hi,

That is correct. But I can give some specific value like index and retrieve it back.

VinExpert
> But I can give some specific value like index and retrieve it back.

Yeah. Since items can be removed and inserted at any time, you'll have to enumerate all the children and adjust their lParams if you want the lParam to represent the index relative to the other children in the node. That doesn't sound like a very good idea to me.

..B ekiM
Hi mikeblas,

I agree that it is complex, But it can be done. And it can be done easily if U know the items and childs associated (at least I think so :-))

VinExpert
> And it can be done easily if U know the items and childs associated

Not as easily as just counting the position of item within the control.

..B ekiM
Avatar of sun307

ASKER

Thanks a lot VinExpert & Mikeblas !!!
-Sun307