Finding an item in TreeCtrl (recursive function)

This function searches in a tree control for an item's associated data. If an exact data match is found, the matching item is returned, otherwise, NULL is returned.

	if(hti == NULL) return NULL;		
	if(GetItemData( hti ) == dwData)
		Select( hti, TVGN_CARET);
		EnsureVisible( hti );			
                return hti;

	hti = GetChildItem( hti );	
		HTREEITEM hti_res;
		if((hti_res = FindData( hti, dwData)) !=NULL )
		       return hti_res; 

	}while( (hti = GetNextSiblingItem( hti )) != NULL );		
	return NULL;


  • great ..!!!!!!!!!!!!!!!!

    Posted by Legacy on 08/08/2003 12:00am

    Originally posted by: Unmesh

    thanx for code ..

  • Yet Another FindItem, Avoiding Return Value Concerns

    Posted by Legacy on 03/28/2003 12:00am

    Originally posted by: just passing thru

    // this is for a CTreeView setting but can be adapted
    // easily to a subclassed CTreeCtrl. can also be
    // used as a pattern for doing any sort of operation
    // on a tree

    // find an item in tree by its ItemData value
    // entry:
    // foundat NULL
    // startat the node to start searching at (NULL for root)
    // itemdata the itemdata value for the node being searched for
    // as set by SetItemData() or the lParam field in TVITEM
    // exit:
    // foundat NULL if not found else the HTREEITEM where item was found
    // note:
    // assumes itemdata is unique, i.e., all nodes have a different value for
    // this. otherwise finds the first node matching the specified value
    void CMyTreeView::FindItem(DWORD itemdata,HTREEITEM &foundat,HTREEITEM startnode)
    CTreeCtrl & tc = GetTreeCtrl();

    // start at root is required
    if ( startnode == NULL ) {

    // check this node for match
    if ( itemdata == tc.GetItemData(startnode) )
    foundat = startnode;

    // now recurse on all children of this node
    HTREEITEM hchild = tc.GetNextItem(startnode,TVGN_CHILD);
    for ( ; hchild != NULL && foundat == NULL ; ) {
    hchild = tc.GetNextSiblingItem(hchild);

  • Thank U very much

    Posted by Legacy on 12/02/2002 12:00am

    Originally posted by: Bijawara N Krishnaswamy

    Thank u for the code.
    I too tried the similar way but got struck in recursive calls.It really helped me a lot

  • TreeView item Level

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: Umakanth

    In a Microsoft Tree View Control , how can we find a Item level i.e in terms of for ROOT it is 0,next item 1 like this.

    If i select a one item in a treeview control, i want to know that level at which it as selected ?

    How can i find out this, any help ?

    Thank you.


  • Faster way to do search

    Posted by Legacy on 11/13/2000 12:00am

    Originally posted by: Matt Honkanen

    If you have a lot of entries in your tree then you might want to consider using a seperate CMap object to help when it comes time to do searching within the tree. This will yield extremely fast search speeds at the small expense of some additional overhead in the form of a map.

  • This algorithm only half works.

    Posted by Legacy on 07/31/2000 12:00am

    Originally posted by: lauraf

    This method does only half of what was asked for. If all you need is to perform an operation on the sought-for HTREEITEM, you're fine. The part of the method that calls Select and EnsureVisible works.

    However, do not rely on the HTREEITEM returned by this method. It is not the right one. The right one gets overwritten as the recursion unwinds -- a common problem in recursive algorithms.

    The method in Ridwan's comment returns the correct HTREEITEM.

  • How about this ...

    Posted by Legacy on 04/24/2000 12:00am

    Originally posted by: Ridwan

    HTREEITEM CGT_treeCtrl::FindAnItem(HTREEITEM hItem, DWORD lKey) 
    HTREEITEM theItem = NULL;

    if (hItem == NULL) return NULL;

    if ((theItem = m_Tree.GetNextSiblingItem(hItem)) != NULL) {
    theItem = FindAnItem(theItem,lKey);
    if (theItem != NULL) return theItem;

    if ((theItem = m_Tree.GetChildItem(hItem)) != NULL) {
    theItem = FindAnItem(theItem,lKey);
    if (theItem != NULL) return theItem;

    if (m_Tree.GetItemData(hItem) == lKey) return hItem;
    if (theItem == NULL) return NULL;
    return theItem;


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • For many organizations, moving enterprise applications to the public cloud can be a very attractive proposition, but planning the best way to move your applications is mission–critical. As an alternative to the costly option of re–architecting the application for a cloud environment, you can follow a "lift and shift" model that's significantly cheaper and almost always a lot quicker. In order to have a successful "lift and shift" migration, read this white paper to learn a few rules you should …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date