Solved

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

Posted on 2002-05-06
7
660 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

24 Experts available now in Live!

Get 1:1 Help Now