Question

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?
}
Was it helpful?

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);

OTHER TIPS

You are trying to reinvent what common controls library already can do for you.

What you need to do is, when you insert a "folder" item set itemex.cChildren = I_CHILDRENCALLBACK which will tell the tree to send you TVN_GETDISPINFO notification when it needs to know if the item has children. It will then similarly send TVN_GETDISPINFO for every individual child.

It will only send the notifications when it is absolutely necessary, so you won't need to do any expensive stuff in vain.

I would say, you need to change the ItemState: http://msdn.microsoft.com/de-de/library/ce034e69%28v=vs.80%29.aspx

BOOL SetItemState( HTREEITEM hItem, UINT nState, UINT nStateMask );

Take a look at the HTREEITEM:

typedef struct tagTVITEM {
  UINT      mask;
  HTREEITEM hItem;
  UINT      state;
  UINT      stateMask;
  LPTSTR    pszText;
  int       cchTextMax;
  int       iImage;
  int       iSelectedImage;
  int       cChildren;
  LPARAM    lParam;
} TVITEM, *LPTVITEM;

cChildren Type: int

Flag that indicates whether the item has associated child items. This member can be one of the following values. 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top