|
-
October 27th, 2005, 11:41 PM
#1
Get index of an item in a TreeCtrl
Hi,
I have a treectrl as shown below
- Root1
|
----- - A
| |
| ----- A1
|
----- B
|
----- C
+ Root2
+ Root3
Here there are three levels.
1. Root level ( Root1, Root2 and Root3)
2. Child first level( A, B and C) and
3. Child second level( A1)
My requirement is, when I am selecting I wantto get the index of that item in its level.
For eg: If I select Root2 or B, I want to get the index as 2
If I select Root1 or A1, I want to get the index as 1
If u have any idea pls help
Ajay
-
October 27th, 2005, 11:44 PM
#2
Re: Get index of an item in a TreeCtrl
Sorry , the example tree diagram I have shown is disorderd.
Pls find the orderd diagram below
- Root1
|
| - A
| |
| - A1
|
| - B
|
| - C
+ Root2
+ Root3
-
October 29th, 2005, 07:44 AM
#3
Re: Get index of an item in a TreeCtrl
All you have to do is get the parent of the currently selected item and then just index down the child list of the parent. It would be something like this ...
Code:
int get_selected_index()
{
int rtn = 0;
HTREEITEM selected_item = tree.GetSelectedItem();
if(selected_item)
{
HTREEITEM parent_item = tree.GetParent(selected_item);
if(parent_item)
{
int index = 1;
HTREEITEM cur_child_item = tree.GetChildItem(hmyItem);
while(cur_child_item)
{
if(cur_child_item == selected_item)
{
rtn = index;
break;
}
cur_child_item = tree.GetNextItem(cur_child_item, TVGN_NEXT);
++index;
}
}
}
return rtn;
}
Good Luck!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|