[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1550
  • Last Modified:

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
0
sun307
Asked:
sun307
  • 4
  • 4
1 Solution
 
Vinayak KumbarSr Program ManagerCommented:
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
0
 
mikeblasCommented:
It's hard for me to guess why that would be useful for you to have, but if you want it, I guess you need it.

The tree control doesn't care much about item indexes. So, you need to loop around, calling GetNextItem() to find where you're at.

Given a particular item hItem, you can find the index of that item under its current level with this code:

   HTREEITEM hFindItem = hItem;
   int nIndex = 0;
   while ( (hFindItem = pCtrl->GetNextItem(hFindItem, TVGN_PREVIOUS)) != NULL)
   {
      nIndex++;
   }

   TRACE("index = %d\n", nIndex);

Yes, it's slow. But that's the only way to do it.

..B ekiM
0
 
Vinayak KumbarSr Program ManagerCommented:
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

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
mikeblasCommented:
> One more method, to get index.

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

..B ekiM
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

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

VinExpert
0
 
mikeblasCommented:
> 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
0
 
Vinayak KumbarSr Program ManagerCommented:
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
0
 
mikeblasCommented:
> 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
0
 
sun307Author Commented:
Thanks a lot VinExpert & Mikeblas !!!
-Sun307
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now