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

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

  • On-demand EventEvent Date: March 21, 2017 With the rise in ransomware attacks, bank heists that reached new levels of sophistication, and extortion plots that were beyond anything we could have imagined, 2016 certainly was an eventful year for cybersecurity. Going into 2017, no business can afford to be uninformed about cybersecurity or unprepared for an attack. Watch this informative webinar, presented by Andrey Pozhogin, Senior B2B Product Marketing Manager at Kaspersky Lab, as he examines predictions for …

Most Popular Programming Stories

More for Developers

RSS Feeds

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