Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

MFC CTreeCtrl messages: Making it so that only one node can be expanded at a time

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:

BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
      ON_NOTIFY_REFLECT(TVN_SELCHANGED, &CLeftView::OnSelchangedTree)
      ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, &CLeftView::OnExpandTree)
END_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;
            return;
      }
      if (CTREEITEM == pCtrl->GetSelectedItem())
      {
      return;
      }
      
      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
0
sternocera
Asked:
sternocera
  • 4
  • 3
1 Solution
 
ZoppoCommented:
Hi,

you can implement a handler for TVN_ITEMEXPANDING notification - if you return TRUE there (through setting '*pResult = 1') you can prevent expanding/collapsing.

Hope that helps,

ZOPPO
0
 
sternoceraAuthor Commented:
Zoppo,

I just want to prevent collapsing. What you've suggested would totally prevent all expanded and collapsing, by the user and by the program.

Thanks
0
 
ZoppoCommented:
From the passed NM_TREEVIEW you can determine the item which is going to be collapsed/expanded, from action you can find if it's gonna collapsed or expanded (TVE_COLLAPSE or TVE_EXPAND).

So you can set *pResult to 1 only if action is TVW_COLLAPSED ...

ZOPPO
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sternoceraAuthor Commented:
So how would the function actually look? what logical check could I perform to see if it's collapsed or expanded?
0
 
ZoppoCommented:
Hi,

should look somehow like this:

void CLeftView::OnItemexpandingTree(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
      
      *pResult = 0;

      if ( pNMTreeView->action & TVE_COLLAPSE )
      {
            *pResult = 1;
      }
}

I made a little test and seems to work fine - it's still possible to collapse items with 'Expand' function, but not with mouse or keyboard.

Hope that helps,

ZOPPO
0
 
sternoceraAuthor Commented:
Zoppo,

That works perfectly. Thank you very much,

Sternocera
0
 
ZoppoCommented:
You're welcome ...

Have a nice day,

regards,

ZOPPO
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now