Solved

How to disable the sub menuitem of the main menu  in an SDI application?

Posted on 2002-05-06
7
658 Views
Last Modified: 2013-11-20
How to disable the sub menuitem of the main menu  in an SDI application? I tried with the following code its not working.

     CMenu* mmenu = GetMenu();
     CMenu* submenu = mmenu->GetSubMenu(0);
     submenu->EnableMenuItem(ID_VIEW_ITEM2, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

Can anyone help me with some explanation.
Thanks in Advance.
0
Comment
Question by:bkkabi
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
VinExpert earned 25 total points
ID: 6991156
Hi,

Instead try the same code with MF_BYPOSITION. It should work. I had faced this problem and fixed it in that way.

Like :

CMenu* mmenu = AfxGetMainWnd()->GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
submenu->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED);

Here, the sub menu item whoes position is 1 will be disabled.

Try it out.
VinExpert
0
 
LVL 3

Assisted Solution

by:Crius
Crius earned 25 total points
ID: 6991217
You have to make sure you are referencing the proper submenu. In your example:

    CMenu* mmenu = GetMenu();
    CMenu* submenu = mmenu->GetSubMenu(0);
    submenu->EnableMenuItem(ID_VIEW_ITEM2, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

I noticed you are referring to GetSubMenu(0). In default MFC applications, this refers to the File menu. However, I noticed you are trying to disable the menu ID "ID_VIEW_ITEM2" which looks like a View menu item. The default View menu item is usually the 3rd or so menu item.

Try:

    CMenu* mmenu = GetMenu();
    CMenu* submenu = mmenu->GetSubMenu(2);
    submenu->EnableMenuItem(ID_VIEW_ITEM2, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

Since we expect the View menu to be at GetSubMenu(2)...
0
 
LVL 4

Expert Comment

by:mblat
ID: 6991379
I think you may have to disable m_bAutoMenuEnable  member of CFrameWnd.  Otherwise enabling/disabling menu items handles  automatically

from MSDN:

CMainFrame::CMainFrame()
{
   // Set to FALSE so no ON_UPDATE_COMMAND_UI or
   // ON_COMMAND handlers are needed, and
   // CMenu::EnableMenuItem() will work as expected.
   m_bAutoMenuEnable  = FALSE;
}
0
 
LVL 3

Expert Comment

by:Crius
ID: 6991737
That could cause a few problems, but works better than manually doing it.

The best way to do it is to handle the On_Update messages sent for than menu item.

     ON_UPDATE_COMMAND_UI(ID_VIEW_ITEM2, OnUpdateViewItem2)


void CMyClass::OnUpdateViewItem2(CCmdUI* pCmdUI)
{
    //You can replace false with a variable to keep track of whether you want the menu enabled or not
    pCmdUI->Enable(false);
}
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 7012049
Hi,

U got ur prob solved? If u have any questions, u may ask them here.

VinExpert
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now