How to populate a tree control
-
Hi, I have a dialog box, I've placed a tree control in it. How can I populate that tree?I need to place items from a .dat file into that tree? I just don't know the basic syntax of doing it... How do you add items? Thanks! P.S. I'm new at this. Tried to use a sample from msdn, but the only thing that it does is gives me a tree with only "united states" in it. Thanks again // Gain a pointer to our tree control CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1); ASSERT(pCtrl != NULL); // Insert a root item using the structure. We must // initialize a TVINSERTSTRUCT structure and pass its // address to the call. TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("United States"); HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert); // Insert subitems of that root. Pennsylvania is // a state in the United States, so its item will be a child // of the United States item. We won't set any image or states, // so we supply only the TVIF_TEXT mask flag. This // override provides nearly complete control over the // insertion operation without the tedium of initializing // a structure. If you're going to add lots of items // to a tree, you might prefer the structure override // as it affords you a performance win by allowing you // to initialize some fields of the structure only once, // outside of your insertion loop. HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT, _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL); // Insert the "Washington" item and assure that it is // inserted after the "Pennsylvania" item. This override is // more appropriate for conveniently inserting items with // images. HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"), 0, 0, hCountry, hPA); // We'll add some cities under each of the states. // The override used here is most appropriate // for inserting text-only items. pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT); pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT); pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT);
-
Hi, I have a dialog box, I've placed a tree control in it. How can I populate that tree?I need to place items from a .dat file into that tree? I just don't know the basic syntax of doing it... How do you add items? Thanks! P.S. I'm new at this. Tried to use a sample from msdn, but the only thing that it does is gives me a tree with only "united states" in it. Thanks again // Gain a pointer to our tree control CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1); ASSERT(pCtrl != NULL); // Insert a root item using the structure. We must // initialize a TVINSERTSTRUCT structure and pass its // address to the call. TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("United States"); HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert); // Insert subitems of that root. Pennsylvania is // a state in the United States, so its item will be a child // of the United States item. We won't set any image or states, // so we supply only the TVIF_TEXT mask flag. This // override provides nearly complete control over the // insertion operation without the tedium of initializing // a structure. If you're going to add lots of items // to a tree, you might prefer the structure override // as it affords you a performance win by allowing you // to initialize some fields of the structure only once, // outside of your insertion loop. HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT, _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL); // Insert the "Washington" item and assure that it is // inserted after the "Pennsylvania" item. This override is // more appropriate for conveniently inserting items with // images. HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"), 0, 0, hCountry, hPA); // We'll add some cities under each of the states. // The override used here is most appropriate // for inserting text-only items. pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT); pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT); pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT); pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT);
Ah, the basic problem of the parent doesn't know that it has any children attached. Change your tvInsert.mask to include TVIF_CHILDREN and set the tvInsert.cChildren either true or false depending on if the parent has any children. Another way is to create parents with no children, then when you add children to a parent, tell the parent that it now has children. When you add a child to a parent, call a function like UpdateParentItem( HTREEITEM hParent ); void UpdateParentItem( HTREEITEM hParent ) { // Can't use ItemHasChildren() because it will always return // the value of the item.cChildren variable. // Need to get the first child and update the parent as needed. // Getting the first child of a parent will always return either // NULL if no children exist or the handle to the first child // regardless of the item.cChildren variable. HTREEITEM hChild = m_pTree->GetChildItem( hParent ); // Set up the item structure TV_ITEM item; ZeroMemory( &item, sizeof(TV_ITEM) ); item.hItem = hParent; item.mask = TVIF_CHILDREN; item.cChildren = hChild != NULL; // Update the parent node to reflect the children state. m_pTree->SetItem( &item ); } Also make sure that when the tree control is created you set the TVS_HASBUTTONS or you will not see the plus/minus button on the parent. You would have to double click the parent to see if there is any children.