CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by VictorN View Post
    Please, explain more clear what you need!
    Quote Originally Posted by sunnysky View Post
    I have two menu items. When item 1 is updated, I want item 2 to be enabled/disabled. In the OnUpdate handler of menu item 1, I have tried to use "t_pMenu = pCmdUI->m_pMenu;", "t_pMenu = pCmdUI->m_pSubMenu;" and "t_pMenu = pCmdUI->m_pParentMenu;" but I always get NULL t_pMenu. How can I achieve this purpose?

    Code:
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowMenuItem1)
        {
            pCmdUI->SetText("Hide Features")
            CMenu * t_pMenu = pCmdUI->m_pSubMenu;
            if(t_pMenu != NULL)
                t_pMenu->EnableMenuItem(ID_MENU_ITEM2, MF_ENABLED);
        }
        else 
        {
            pCmdUI->SetText("Show Features")
            CMenu * t_pMenu = pCmdUI->m_pParentMenu;
            if(t_pMenu != NULL)
                t_pMenu->EnableMenuItem(ID_MENU_ITEM2, MF_GRAYED);
        }
    }
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        ...
    }

    I asked you for more clear explanation of your problem, not just copy/paste of your OP!

    How could we help you if you didn't explain your problem?
    Victor Nijegorodov

  2. #17
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Hi GCDEF,

    I don't get method 1 using same handler. Menu item 1 is clicked, so OnUpdateMenu1() is called. But when I click menu item 2, I don't want OnUpdateMenu1() to be called. I don't understand the purpose here.

    Now method 2 using state variable. I updated the state variable m_bShowFeatures in the OnMenuItem1, but OnUpdateMenuItem2 still doesn't get called.

    Basically, what we want is that when Menu 1 is set to be "Show Features", Menu 2 is completely disabled. When Menu 1 is set to be "Hide Features", Menu 2 can be set to be "Show..." or "Hide..." itself.

    Below is more code. I hope this provides more context. Thanks a lot.

    Code:
    void CDummyView::OnMenuItem1()
    {
        m_bShowFeatures = !m_bShowFeatures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_ALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowFeatures)
        {
            pCmdUI->SetText("Hide Features")
            CMenu * t_pMenu = pCmdUI->m_pSubMenu;
            if(t_pMenu != NULL)
                t_pMenu->EnableMenuItem(ID_MENU_ITEM2, MF_ENABLED);
        }
        else 
        {
            pCmdUI->SetText("Show Features")
            CMenu * t_pMenu = pCmdUI->m_pParentMenu;
            if(t_pMenu != NULL)
                t_pMenu->EnableMenuItem(ID_MENU_ITEM2, MF_GRAYED);
        }
    }
    
    void CDummyView::OnMenuItem2()
    {
        m_bShowSmallFetures= !m_bShowSmallFetures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_SMALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        if(m_bShowSmallFetures)
            pCmdUI->SetText("Hide Small Features")
        else 
            pCmdUI->SetText("Show Small Features")
    }

  3. #18
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    I changed the OP.

  4. #19
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    It's not a copy/paste. I made it clearer in the next post.

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by sunnysky View Post
    Hi GCDEF,

    I don't get method 1 using same handler. Menu item 1 is clicked, so OnUpdateMenu1() is called. But when I click menu item 2, I don't want OnUpdateMenu1() to be called. I don't understand the purpose here.

    Now method 2 using state variable. I updated the state variable m_bShowFeatures in the OnMenuItem1, but OnUpdateMenuItem2 still doesn't get called.

    Basically, what we want is that when Menu 1 is set to be "Show Features", Menu 2 is completely disabled. When Menu 1 is set to be "Hide Features", Menu 2 can be set to be "Show..." or "Hide..." itself.

    Below is more code. I hope this provides more context. Thanks a lot.

    Code:
    void CDummyView::OnMenuItem1()
    {
        m_bShowFeatures = !m_bShowFeatures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_ALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowFeatures)
        {
            pCmdUI->SetText("Hide Features")
         }
        else 
        {
            pCmdUI->SetText("Show Features")
        }
    }
    
    void CDummyView::OnUpdateMenuItem2()
    {
        m_bShowSmallFetures= !m_bShowSmallFetures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_SMALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        if(m_bShowSmallFetures)
            pCmdUI->SetText("Hide Small Features")
        else 
            pCmdUI->SetText("Show Small Features")
    }
    Wouldn't this do what you want?
    Code:
    void CDummyView::OnMenuItem1()
    {
        m_bShowFeatures = !m_bShowFeatures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_ALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowFeatures)
        {
            pCmdUI->SetText("Hide Features")
         }
        else 
        {
            pCmdUI->SetText("Show Features")
        }
    }
    
    
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(!m_bShowSmallFetures)
    }

  6. #21
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by GCDEF View Post
    Wouldn't this do what you want?
    Code:
    void CDummyView::OnMenuItem1()
    {
        m_bShowFeatures = !m_bShowFeatures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_ALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowFeatures)
        {
            pCmdUI->SetText("Hide Features")
         }
        else 
        {
            pCmdUI->SetText("Show Features")
        }
    }
    
    
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(!m_bShowSmallFetures)
    }
    Thanks.

    But this probably doesn't work. In this case, when Menu Item 2 is grayed, it cannot be enabled again.

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

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by sunnysky View Post
    Thanks.

    But this probably doesn't work. In this case, when Menu Item 2 is grayed, it cannot be enabled again.
    Did you try it? When you call menu1 again, m_bShowFeatures will be set to the opposite value, and menu 2 will be enabled again. Menu item 2 is grayed out or not based on the value of m_bShowFeatures.

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by sunnysky View Post
    Thanks.

    But this probably doesn't work. In this case, when Menu Item 2 is grayed, it cannot be enabled again.
    How about debug it?

    For example, put a breakpoint on the Enable call.

    Code:
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(!m_bShowSmallFetures)
    }
    Then as a test, replace

    Code:
    pCmdUI->Enable(!m_bShowSmallFetures);
    with

    Code:
    pCmdUI->Enable(TRUE);
    That way, you can see whether it's a command UI issue or because m_ShowSmallFetures isn't getting set properly.

  9. #24
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Sorry for the mistake in the previous post and this repost. I can't edit my posts.

    I got it work. Below is the code I tried. Two flag variables m_bShowFeatures and m_bShowSmallFetures are initialized to be TRUE.


    Code:
    void CDummyView::OnMenuItem1()
    {
        m_bShowFeatures = !m_bShowFeatures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_ALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI) 
    {
        if(m_bShowFeatures)
            pCmdUI->SetText("Hide Features")
        else 
            pCmdUI->SetText("Show Features")
    }
    
    void CDummyView::OnMenuItem2()
    {
        m_bShowSmallFetures= !m_bShowSmallFetures;
        m_pDoc->UpdateAllViews(NULL, SHOW_HIDE_SMALL_FEATURES);
    }
    
    void CDummyView::OnUpdateMenuItem2(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(m_bShowFetures)
        if(m_bShowSmallFetures)
            pCmdUI->SetText("Hide Small Features")
        else 
            pCmdUI->SetText("Show Small Features")
    }
    So OnUpdateMenuItem2() does get invoked after OnMenuItem1() is called when Menu Item 1 is clicked. I didn't expect this.

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    Quote Originally Posted by sunnysky View Post
    Sorry for the mistake in the previous post and this repost. I can't edit my posts.
    I guess it is because you logged in as a Guest rather than a registered CG user.
    Victor Nijegorodov

  11. #26
    sunnysky Guest

    Re: How to Enable/Disable Menu Item 2 in OnUpdate Handler of Menu Item 1?

    In "My Profile", I am shown as a member. But I don't know why I am a "Guest" when posting.

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured