Finding an item (matching data instead of label)

FindItemData is another tree control searching function very similar to the FindItem function. Instead of matching a substring of the label, it compares the item's associated data. If an exact data match is found, the matching item is returned, otherwise NULL. This function uses the functions GetNextItem, GetPrevItem, and GetLastItem seen previously. In order to use this function, your tree control items must have associated data, which can be set using SetItemData. Also, the item's mask must include TVIF_PARAM.
// FindItemData  - Finds an item whose data matches the passed in lparam value.
// Returns       - Handle to the item or NULL
// lparam        - Data to search for
// bDownDir      - Search direction - TRUE for down
// hItem         - Item to start searching from. NULL for currently selected item

HTREEITEM CTreeCtrlX::FindItemData(DWORD lparam, BOOL bDownDir /*=TRUE*/, HTREEITEM hItem /*=NULL*/)
{
	HTREEITEM htiSel = hItem ? hItem : GetSelectedItem();
	HTREEITEM htiCur = bDownDir ? GetNextItem( htiSel ) :
	GetPrevItem( htiSel );
	if( htiCur == NULL )
	{
		if( bDownDir )
			htiCur = GetRootItem();
		else
			htiCur = GetLastItem( NULL );
	}
	while( htiCur && htiCur != htiSel )
	{
		DWORD sItemData = GetItemData( htiCur );
		if (sItemData == lparam )
			return htiCur;
		htiCur = bDownDir ? GetNextItem( htiCur ) : GetPrevItem( htiCur );
		if( htiCur == NULL )
		{
			if( bDownDir )
				htiCur = GetRootItem();
			else
				htiCur = GetLastItem( NULL );
		}
	}
	return NULL;
}



Comments

  • Same idea ... less code

    Posted by Legacy on 06/25/2002 12:00am

    Originally posted by: Shaun Cooley

    HTREEITEM CTreeCtrlEx::FindItemData(DWORD dwData, HTREEITEM hItem /* = TVI_ROOT */)
    
    {
    if(!hItem)
    return NULL;

    if(GetItemData(hItem) == dwData)
    return hItem;

    HTREEITEM hRet = NULL;
    HTREEITEM hChild = GetChild(hItem);
    if(hChild)
    hRet = FindItemData(dwData, hChild);

    if(hRet == NULL)
    {
    HTREEITEM hSibling = GetNextSibling(hItem);
    if(hSibling)
    hRet = FindItemData(dwData, hSibling);
    }

    return hRet;
    }

    Reply
  • Scanning a tree is very processor time expensive.

    Posted by Legacy on 01/22/1999 12:00am

    Originally posted by: Patrick Laplante

    This is not an efficient way to do it.
    Think about it.... the more item you got, the longer it will take.

    Use a CMap instead.
    CMap<HTREEITEM, HTREEITEM&, CMyData*, CMyData*> myMap;

    And look the help file on how to use it.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds