Dave's Brain

Browse - Programming Tips - What does CTreeCtrl::GetChildItem() do?

Date: 2017aug28
OS: Windows
Language: C++
Framework: MFC

Q.  What does CTreeCtrl::GetChildItem() do?

A.  It gets the *first* child of the input hItem.
You can not call it repeatedly to get the next child.
It should have been called GetFirstChildItem().

The comment in some versions of afxcmn.h is wrong. eg:

// Retrieves the next child item after the specified item. <-- wrong HTREEITEM GetChildItem(_In_ HTREEITEM hItem) const;
To get remaining children you can repeatedly call GetNextItem(, TVGN_NEXT)
void ListAllChildren(CTreeCtrl &tree; HTREEITEM hItem) { for (HTREEITEM hChild = tree.GetChildItem(hItem); hChild != NULL; hChild = tree.GetNextItem(hChild, TVGN_NEXT)) { // Do something with hChild } }

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.