Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SelectItem of CTreeCtrl

SelectItem of CTreeCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasehelptutorial
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zon_cpp
    wrote on last edited by
    #1

    Hi! how do i select 3th item of a CTreeCtrl? SelectItem() function needs HTREEITEM of specified item, but i have index of item (for example 3th item) please help me...

    Zo.Naderi-Iran

    Z R 2 Replies Last reply
    0
    • Z zon_cpp

      Hi! how do i select 3th item of a CTreeCtrl? SelectItem() function needs HTREEITEM of specified item, but i have index of item (for example 3th item) please help me...

      Zo.Naderi-Iran

      Z Offline
      Z Offline
      Zasky
      wrote on last edited by
      #2

      It's not possible to access nodes by index. You must know the handle of the node. If you don't have records of the node handles, you can parse the tree:

      // Start with the root item:
      HTREEITEM hRoot = myTree.GetRootItem();

      // Get the first child of the root:
      HTREEITEM hChild1 = myTree.GetChildItem(hRoot);

      // Move through the siblings until you reach the node you want:
      HTREEITEM hChild2 = myTree.GetNextSiblingItem(hChild1)
      HTREEITEM hChild3 = myTree.GetNextSiblingItem(hChild2)

      1 Reply Last reply
      0
      • Z zon_cpp

        Hi! how do i select 3th item of a CTreeCtrl? SelectItem() function needs HTREEITEM of specified item, but i have index of item (for example 3th item) please help me...

        Zo.Naderi-Iran

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        You could do something like this:

        HTREEITEM ItemFromIndex(int index)
        {
        HTREEITEM item = m_tree.GetRootItem();
        while (item != NULL && index > 0)
        {
        item = NextTreeItem(item);
        --index;
        }
        // if item is not NULL, you now have the HTREEITEM of the given index
        return item;
        }

        HTREEITEM NextTreeItem(HTREEITEM hStartItem)
        {
        HTREEITEM hItem = hStartItem;
        if (m_tree.ItemHasChildren(hItem))
        {
        // move down to next level
        HTREEITEM hChildItem = m_tree.GetChildItem(hItem);
        hItem = hChildItem;
        }
        else
        {
        bool foundSibling = false;
        while (!foundSibling && hItem != NULL)
        {
        // move along same level
        HTREEITEM hSiblingItem = m_tree.GetNextItem(hItem, TVGN_NEXT);
        if (hSiblingItem != NULL)
        {
        hItem = hSiblingItem;
        foundSibling = true;
        }
        else
        {
        // nothing on same level so move up and try again
        hItem = m_tree.GetParentItem(hItem);
        }
        }
        }
        return hItem;
        }

        Som eof this was written from memory, other sections are code pasts, so take with a pinch of salt.

        If you vote me down, my score will only get lower

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups