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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds