Finding an item (matching data instead of label)
Posted
by Nancy Davis
on August 6th, 1998
// 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:00amOriginally posted by: Shaun Cooley
ReplyScanning a tree is very processor time expensive.
Posted by Legacy on 01/22/1999 12:00amOriginally 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