-
March 20th, 2019, 10:39 PM
#1
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
-
March 21st, 2019, 02:52 AM
#2
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.
-
March 21st, 2019, 05:57 AM
#3
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
-
March 21st, 2019, 06:47 AM
#4
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.
-
March 21st, 2019, 07:07 AM
#5
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 21st, 2019, 09:31 AM
#6
Re: EnableMenuItem problem
Originally Posted by Arjay
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.
-
March 21st, 2019, 10:39 AM
#7
Re: EnableMenuItem problem
See Arjay's post. Don't call the handler directly. The framework will call it when it needs to.
-
March 21st, 2019, 02:25 PM
#8
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.
-
March 22nd, 2019, 12:32 AM
#9
Re: EnableMenuItem problem
Originally Posted by robertzp
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.
-
March 22nd, 2019, 04:17 AM
#10
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
-
March 22nd, 2019, 09:34 AM
#11
Re: EnableMenuItem problem
Originally Posted by mesajflaviu
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.
-
March 22nd, 2019, 10:26 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|