dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: EnableMenuItem problem

  1. #1
    Join Date
    Jul 2016
    Posts
    35

    EnableMenuItem problem

    Hello,
    I have an MDI application and having problems disabling or graying an item in the drop-down menu. I am using visual studio 2015

    The drop-down menu is fourth from the left. Zero-based 3. The code is:

    Code:
    CWnd* pMain = AfxGetMainWnd();
    
    if (pMain != NULL)
    {
      CMenu* pMenu = pMain->GetMenu()->GetSubMenu(3);  
    
      // following test count agrees with number of drop-down menu items
      int count = pMenu->GetMenuItemCount();	
    
      if (pMenu != NULL && pMenu->GetMenuItemCount() > 0)
     {
        BOOL res = pMenu->EnableMenuItem(ID_ANALYZE, MF_BYCOMMAND | MF_GRAYED);
    
        pMain->DrawMenuBar();
     }
    BOOL res returns 0 which indicates to me that the EnableMenuItem command was successful.

    But the menu item is not GRAYED!

    I've tried different methods including by position, but nothing works! For example,

    Code:
    BOOL res = pMenu->EnableMenuItem(ID_MENUID, MF_BYCOMMAND | MF_GRAYED |MF_DISABLED);
    
    BOOL res = pMenu->EnableMenuItem(3, MF_POSITION | MF_DISABLED);
    Any suggestions?

    Thank you in advance
    Last edited by 2kaud; March 21st, 2019 at 04:10 AM. Reason: Added code tags

  2. #2
    Join Date
    Jan 2009
    Posts
    291

    Re: EnableMenuItem problem

    Try to enable/disable those menu item trough message map, just like this:
    Code:
    BEGIN_MESSAGE_MAP(CYourView, CListView)
    	ON_UPDATE_COMMAND_UI(ID_MENUID, OnUpdateItem)
    END_MESSAGE_MAP()
    Last edited by mesajflaviu; March 21st, 2019 at 02:56 AM.

  3. #3
    Join Date
    Jul 2016
    Posts
    35

    Re: EnableMenuItem problem

    I have been trying to do this

    Code:
    BEGIN_MESSAGE_MAP(….)
    ON_UPDATE_COMMAND_UI(ID_MENUID, OnUpdateShowResults)
    
    void CAppDoc::OnUpdateShowResults(CCmdUI* pCmdUI)
    {
    	pCmdUI->Enable(FALSE);
    }
    But I can’t find the correct code way to issue the OnUpdateShowResults() command inside the code.

    But the following generates an exception.
    Exception thrown: read access violation.
    pCmdUI was nullptr

    OnUpdateShowResults(FALSE);
    Last edited by 2kaud; March 21st, 2019 at 07:07 AM. Reason: Added code tags

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,105

    Re: EnableMenuItem problem

    Are you calling the handler OnUpdateShowResults(FALSE); directly? If so, you shouldn't be calling this directly because the framework will call this for you since you have added an ON_COMMAND_UPDATE_UI map entry. Put a breakpoint inside the handler to make sure it is getting called.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,874

    Re: EnableMenuItem problem

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#'].

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.4)

  6. #6
    Join Date
    Jul 2016
    Posts
    35

    Re: EnableMenuItem problem

    Quote Originally Posted by Arjay View Post
    Are you calling the handler OnUpdateShowResults(FALSE); directly? If so, you shouldn't be calling this directly because the framework will call this for you since you have added an ON_COMMAND_UPDATE_UI map entry. Put a breakpoint inside the handler to make sure it is getting called.
    The whole point is for the program to be able to disable/enable a drop-down menu item directly without user intervention.

    For which reason I was trying unsuccessfully to do this thru the CMenu class. Please see my opening post to this thread.

    Per suggestion by mesajflaviu, I began trying to accomplish this thru the message map and by calling the handler directly.

    I had placed a breakpoint at the handler and the read access violation occurs at:

    Code:
    pCmdUI->Enable(FALSE);
    thank you...
    Last edited by robertzp; March 21st, 2019 at 09:44 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,551

    Re: EnableMenuItem problem

    See Arjay's post. Don't call the handler directly. The framework will call it when it needs to.

  8. #8
    Join Date
    Jul 2016
    Posts
    35

    Re: EnableMenuItem problem

    The only way to call the handler INDIRECTLY is for the user to click on the menu item.

    But as I have been trying to explain, that is what I am trying to prevent to begin with by disabling it programatically.

    I am back to square one. There may be a way to get the CMenu - EnableMenuItem to work.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,105

    Re: EnableMenuItem problem

    Quote Originally Posted by robertzp View Post
    The only way to call the handler INDIRECTLY is for the user to click on the menu item.

    But as I have been trying to explain, that is what I am trying to prevent to begin with by disabling it programatically.

    I am back to square one. There may be a way to get the CMenu - EnableMenuItem to work.
    You are coding it wrong; otherwise, your update ui handler would get called as the menu item is getting displayed and the menu item would be disabled.

    Oh, and I get that you think no one UNDERSTANDS your problem even if a few of us answering have programmed in MFC for 20+ years.

  10. #10
    Join Date
    Jan 2009
    Posts
    291

    Re: EnableMenuItem problem

    If you want to enable / disable that menu item programmatically then you only made a condition inside your UpdateMenu handler ... something like this:
    Code:
    void CAppDoc::OnUpdateShowResults(CCmdUI* pCmdUI)
    {
    	pCmdUI->Enable(m_bCond);
    }
    and somewhere in your app:
    Code:
    m_bCond = TRUE // or FALSE;
    and framwork will do the job

  11. #11
    Join Date
    Jul 2016
    Posts
    35

    Re: EnableMenuItem problem

    Quote Originally Posted by mesajflaviu View Post
    If you want to enable / disable that menu item programmatically then you only made a condition inside your UpdateMenu handler ... something like this:
    Code:
    void CAppDoc::OnUpdateShowResults(CCmdUI* pCmdUI)
    {
    	pCmdUI->Enable(m_bCond);
    }
    and somewhere in your app:
    Code:
    m_bCond = TRUE // or FALSE;
    and framework will do the job
    My apologies to all.

    I made a mistake in coding the message handler. The menu items are now being enabled / disabled programatically.

    thanks for your time.
    Last edited by robertzp; March 23rd, 2019 at 04:59 PM.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,105

    Re: EnableMenuItem problem

    Zip up a small project that shows the reproductive and post it here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)