3

I have a tree view (CTreeView) that will show me a pop-up menu after I right-click my mouse on it. In my context menu there are only 3 items (i.e A, B, C) for selection and my tree view displays a long list of ordered foods designed with check-boxes. I would like to disable menu items A and B if no ordered foods are checked and enable them when any is.

I create CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) //CFoodView inherits CTreeView and CFoodView::OnUpdateItemB(CCmdUI* pCmdUI) to handle their states like so

CFoodView::OnUpdateItemB(CCmdUI* pCmdUI)
{
    if TreeView has no items
    {
        pCmdUI->Enable(FALSE);
    }
    else
    {
        *Search* the tree to get selected items
        if None is checked
        {
            pCmdUI->Enable(FALSE);
        }
        else there are checked items
            pCmdUI->Enable(TRUE);
    }
}

Method CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) is the same.

I think this isn't a correct way to handle this GUI feature.

Hello Everyone
  • 329
  • 4
  • 11

1 Answers1

1

Well, you did not submit all important information. How did you create menu item handlers? Assuming you insert handlers the proper way, still did not provide any information how you are invoking popup menu. If all you did was properly done it is the proper way of handling update menu. The most common mistake is designating view itself as the window that handles popup updates and commands. In order to use MFC menu update mechanism, you have to pass a pointer to the main window not to the tree view:

    CWnd *pMainWnd = AfxGetMainWnd();
    ASSERT(pMainWnd != nullptr);

    pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, pMainWnd);

If this will not work reexamine the way you create handler and/or the place you invoke the TrackPopupMenu function.

JohnCz
  • 1,613
  • 1
  • 9
  • 9
  • Thanks for your reply. Everything works fine. My concern is about the **Search** method that works recursively throughout the tree to retrieve checked nodes and leaves of the tree. I think it may not be good because I allow users to load a lot of data into the tree at the same time. So I asked the above question. I use MSVS 2017's MFC by the way. – Hello Everyone Jul 04 '17 at 18:52