Search code examples
c++mfctreeviewtreeviewitem

Re-expand treeview item


I am working on an old MFC application. There is a TreeView control in the application. OnItemExpanding function is overridden. I am getting children of a TreeViewItem by it is expanded. If a node is expanded for the first time its children are populated. If there are no children to an item then the expand icon (+ sign) removes from the TreeViewItem.

Now problem is that I expanded one node which doesn't have children. After doing some work children are added to that node. But now I cannot get newly added children as expand icon is missing. How I can refresh that particular node in the TreeView. I have created a refresh button. In that I am able to find my current selected node in the TreeView, but what next.

Here is the code.

void CMyTreeView::OnItemExpanding(CTreeCtrl& tree, NMHDR* pNMHDR, LRESULT* pResult) 
{
   //This is only called when I click on expand (+ sign)
   //some check here which populates children.
}

void CMyTreeView::RefreshNode(CTreeCtrl& tree, HTREEITEM selectedNode)
{
    // What should I do here?
}

Solution

  • You have to set cChildren of TVITEM to 'one':

    TVITEM tvItem = {0};
    tvItem.mask   = TVIF_HANDLE | TVIF_CHILDREN;
    tvItem.hItem  = selectedNode;
    tvItem.cChildren = 1;
    tree.SetItem(&tvItem);