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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds