Finding an item in TreeCtrl (recursive function)

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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;
}



Comments

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

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

    Originally posted by: Unmesh

    thanx for code ..

    Reply
  • 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 ) {
    FindItem(itemdata,foundat,tc.GetRootItem());
    return;
    }

    // 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 ; ) {
    FindItem(itemdata,foundat,hchild);
    hchild = tc.GetNextSiblingItem(hchild);
    }
    }


    Reply
  • 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
    -krishna

    Reply
  • 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.

    -Umakanth

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • 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;

    }

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

Top White Papers and Webcasts

  • This case study will highlight the opportunity for mobile and cloud technologies in the Public Sector—specifically for state, local and federal government agencies -where secure mobile application access can offer key productivity gains

  • The software-defined data center (SDDC) and new trends in cloud and virtualization bring increased agility, automation, and intelligent services and management to all areas of the data center. Businesses can now more easily manage the entire lifecycle of their applications and services via the SDDC. This Aberdeen analyst report examines how a strong foundation in both the cloud and internal data centers is empowering organizations to fully leverage their IT infrastructure and is also preparing them to be able …

Most Popular Programming Stories

More for Developers

RSS Feeds

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