MFC CTreeCtrl messages: Making it so that only one node can be expanded at a time
Posted on 2007-07-26
I have a treeview that is used for navigation in a MFC application. I wish to have my tree view have exactly one node only expanded at any given time - when another node is expanded, the previous node is collapsed. Here is my message map:
Here is my OnExpandTree message handler:
void CLeftView::OnExpandTree(NMHDR* pNMHDR, LRESULT* pResult)
if (NotJustStarted == FALSE)
// skip it the first time, so the first node can expand
NotJustStarted = TRUE;
if (CTREEITEM == pCtrl->GetSelectedItem())
pCtrl->Expand(CTREEITEM, TVE_COLLAPSE); // collapses previous node
CTREEITEM = pCtrl->GetSelectedItem(); // sets CTREEITEM to current node
I make the function immediately return the first time it is called by the program, so my first node/HTREEITEM can expand and be set to CTREEITEM (the initial expansion of the node is done in OninitialUpdate).
This is pretty close to the effect I'm going for. Initially, the first node is opened. When I expand another node, the previous node is collapsed. However, it is still possible for the user to collapse a node and have all nodes collapsed. I don't want this. How can I prevent the user from being able to collapse a node?
Thanks in advance