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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds