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
LVL 2
sun307Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.