I have been building a AddressBook program and I want to know how to insert items to a child item in a treeview...

The only problem is that I need to get the child items from the treeview first...

Get the child item...
Check if thats the child item i want
If yes add an item to it...

so how do i use the GetTVMITEM or whatever it is aswell...

Clarence , Thank you for your question.

Tree View Controls are described in the MSDN Library. This comes with your Visual C++ compiler.

To insert a child item into a listview entry, you might use code similar to this (I modified this from the MSDN Library):

// AddItemToTree - adds items to a tree view control.
// Returns the handle to the newly added item.
// hwndTV - handle to the tree view control.
// lpszItem - text of the item to add.
// nLevel - level at which to add the item.
HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel)
   TVITEM tvi;
   static HTREEITEM hPrevRootItem = NULL;
   static HTREEITEM hPrevLev2Item = NULL;

   tvi.mask = TVIF_TEXT | TVIF_PARAM;

   // Set the text of the item.
   tvi.pszText = lpszItem;
   tvi.cchTextMax = lstrlen(lpszItem);

   // Save the heading level in the item's application-defined
   // data area.
   tvi.lParam = (LPARAM) nLevel;

   tvins.item = tvi;
   tvins.hInsertAfter = hPrev;

   // Set the parent item based on the specified level.
   if (nLevel == 1)
       tvins.hParent = TVI_ROOT;
   else if (nLevel == 2)
       tvins.hParent = hPrevRootItem;
       tvins.hParent = hPrevLev2Item;

   // Add the item to the tree view control.
   hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEM, 0,
        (LPARAM) (LPTVINSERTSTRUCT) &tvins);

   // Save the handle to the item.
   if (nLevel == 1)
       hPrevRootItem = hPrev;
   else if (nLevel == 2)
       hPrevLev2Item = hPrev;

   return hPrev;

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.