Finding an item in TreeCtrl (recursive function)

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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.


HTREEITEM CTreeCtrlX::FindData(HTREEITEM hti, DWORD dwData)
{
	if(hti == NULL) return NULL;
	if(GetItemData( hti ) == dwData)
        {
		Select( hti, TVGN_CARET);
		EnsureVisible( hti );
                return hti;
        }

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

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

More by Author

Previous article
Next article

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read