CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2005
    Posts
    111

    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

  2. #2
    Join Date
    Jan 2005
    Posts
    111

    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

  3. #3
    Join Date
    Jul 2003
    Posts
    147

    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
  •  





Click Here to Expand Forum to Full Width

Featured