Does EnableMenuItem() work at all?

What I need is when I select MenuItem1, MenuItem1 is grayed and MenuItem2, which is originally grayed, is enabled.
And my code is as follows.

CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, MFS_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MFS_ENABLED);
SetMenu( pMenu );

But nothing changes!!! Please help!
P.S. I know how to make it work through ON_UPDATE_COMMAND_UI(). I'm just wondering why EnableMenuItem() doesn't work.
shibaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:
Hi,

do the following

CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, MF_BYCOMMAND | MF_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MF_BYCOMMAND | MF_ENABLED);
SetMenu( pMenu );

Try it out.
VinExpert
0
migelCommented:
Hi!
call DrawMenuBar() after menu changing.
0
inprasCommented:
Hi
try
pMenu->EnableMenuItem(2, MF_GRAYED|MF_BYPOSITION) ;
Hope this helps
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Vinayak KumbarSr Program ManagerCommented:
Hi,

try this code
CMenu pMenu;
pMenu.LoadMenu (IDR_MAINFRAME);
pMenu->EnableMenuItem( ID_MenuItem1, MF_BYCOMMAND |
                                MF_GRAYED );
pMenu->EnableMenuItem( ID_MenuItem2, MF_BYCOMMAND |
                                MF_ENABLED);
AfxGetMainWnd()->SetMenu( &pMenu );
AfxGetMainWnd()->DrawMenuBar();
pMenu.Detach ();

VinExpert
0
shibaiAuthor Commented:
Hi, guys. Thanks for all your inputs.
But I've tried all these before I submitted my question. None worked. I increased points to 200. I'll see who can help me out.
0
migelCommented:
Hi!
Where you use this piece of code?
Can you show test project?
0
shibaiAuthor Commented:
Briefly, this is a MDI project. I added a submenu which has two items. I then added message handlers for them. In message handlers, I tried to enable one item and gray the other. That it!

If you need more info, let me know. Thanks!
0
abancroftCommented:
Do you mean when the menu item command is executed or when you scroll through the menu items?
0
Nicolay_ChCommented:
See ON_UPDATE_COMMAND_UI

When MFC want to know status of you menu string or button on any  XxxBar it call function, that handled in this macro...

One Macro for one string or control...

See MSDN for help...
0
mikeblasCommented:
EnableMenuItem() works if you call it correctly, as the other folks have shown.  (And you dont' need to call DrawMenu() unless you're doing this while the menu is actually dropped-down.)

But you're writing an MFC project, which means that you should use MFC's UPDATE_COMMAND_UI mechanism. You should add UPDATE_COMMAND_UI handlers for ID_MenuItem1 and ID_MenuItem2, and then edit the handler so that they do what you want:

void CQwerView::OnUpdateWindowMenuitem1(CCmdUI* pCmdUI)
{
   if (bCondition)
      pCmdUI->Enable(TRUE);
   else
      pCmdUI->Enable(FALSE);
}

This helps make your code more reusable, and is the MFC way. Any good learning book on MFC describes this mechanism.

..B ekiM
0
Nicolay_ChCommented:
Sorry for dummy answer... I not read question closely...

EnableMenuItem not work for this reason... MFC set status of menu strings with macro ON_UPDATE_COMMAND_UI
0
mikeblasCommented:
Which is what I said.

..B ekiM
0
shibaiAuthor Commented:
Sorry, Mike. I don't think I could give you the points. I said in my question I know how to make it work with macro ON_UPDATE_COMMAND_UI. And I still believe there got to be a way to make it work through EnableMenuItem(), though it may not be a good way. Otherwise, why do we need this function EnableMenuItem()?
0
SadhuCommented:
try following.
CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem( ID_MenuItem1, true);
pMenu->EnableMenuItem( ID_MenuItem2, true);

Sadhu
0
migelCommented:
Hi!
MFC implemet "smart" menu enabling
main frame wnd enable menu item if found handler for it(and disable it otherwise). Since you have handlers for these menu items MFC olways enabled it.
You can remove this check by setting CFrameWnd::m_bAutoMenuEnable  to the FALSE.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeblasCommented:
> I said in my question I

Oh! Sorry!  It's amazing what's escaping my eyes these days. I guess I'm getting old.

EnableMenuItem() doesn't work because, after you call EnableMenuItem(), MFC does its ON_UPDATE_COMMAND_UI work and undoes the effect of your direct EnableMenuItem() call.

It's that simple!

You don't "need" EnableMenuItem(). It's provided because there's an API of the same name, and MFC wants to provide that compatibility for you in a much more reusable and object oriented way.

If you want to use EnableMenuItem(), you'll have to get MFC to completely turn off it's ON_UPDATE_COMMAND_UI handling. You can
do that by setting the m_bAutoMenuEnable to FALSE in the constructor of your frame window. MFC will _still_ do most of this processing, though. IF you want to completely halt it, you can handle WM_INITMENUPOPUP and WM_INITMENU and do nothing in response.

..B ekiM
0
shibaiAuthor Commented:
Sorry, Mike. Migel gave the correct answer before you. So I'll have to give the points to Migel to be fair. Thanks, guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.