Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable some context menu items if no items are checked

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.

like image 279
Hello Everyone Avatar asked Jan 22 '26 03:01

Hello Everyone


1 Answers

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.

like image 97
JohnCz Avatar answered Jan 23 '26 16:01

JohnCz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!