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: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds