CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Location
    CA,USA
    Posts
    22

    Finding an item in a Tree Control

    When I am adding second level entries, (the root levels are titles), to my tree view, I get duplicate entries in the Tree.
    So I would like to be able to search through the existing tree items to find if there is a match. However, there is a bewildering array of GetxxxItem() - type functions, which one(s) should I use to do this??


  2. #2
    Join Date
    May 1999
    Posts
    667

    Re: Finding an item in a Tree Control

    Try the following
    this assumes you are stroing an object pointer as the itemdata for an entry, if you are using text just compre the text instead

    HTREEITEM OLITreeView::getTreeItemFor(yourObject * pyo, HTREEITEM item = GetRootItem())
    {
    HTREEITEM childItem, theItem;
    YourObject *tpyo;
    // Is the item null
    if (item == NULL)
    return NULL;

    tpyo = (YourObject*)GetItemData(item);
    // Is the item the one we want
    if (tpyo == pyo)
    return item;

    if(!ItemHasChildren(item))
    return NULL;

    childItem = GetNextItem(item, TVGN_CHILD);
    while (childItem != NULL)
    { // call this function recusivly to check all children
    theItem = getTreeItemFor(pyo, childItem);
    if (theItem !=NULL)
    return theItem;
    childItem = GetNextItem(childItem, TVGN_NEXT);
    }
    return NULL;
    }

    HTH,
    Chris


  3. #3
    Join Date
    Oct 1999
    Location
    CA,USA
    Posts
    22

    Re: Finding an item in a Tree Control

    That worked really well, I refined the code a little as the item I am looking for will always occur in the first level below the root;

    hLev1 = NULL; // Preset to no Level1 Entries
    if (ctc.ItemHasChildren(hRoot)) // If there are any Level1 Entries
    {
    hLev1 = ctc.GetNextItem(hRoot, TVGN_CHILD); // Get the Level1 Handle
    while (hLev1 != NULL)
    {
    cs = ctc.GetItemText(hLev1); // Get the Item Name
    if (m_ArtName == cs) // If there is already a level1 for this Artist
    break; // ..quit
    else
    hLev1 = ctc.GetNextItem(hLev1, TVGN_NEXT);// ..else get the next one
    }
    }
    if (hLev1 == NULL) // If we found no matches
    hLev1 = ctc.InsertItem(m_ArtName, hRoot, TVI_SORT); // Insert new entry into the tree
    hLev2 = ctc.InsertItem(m_CDTitle, hLev1, TVI_SORT); // Add the Title Below

    ctc.SetItemData(hLev2, m_CDKey); // ..and hide the key






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured