Solved

Disableing menuitems on the fly

Posted on 1998-09-08
12
158 Views
Last Modified: 2013-11-20
Dear Experts,
How do you make a menu item into a variable?
And how do you disable the menu item while the program is running.
0
Comment
Question by:ramsay
  • 7
  • 4
12 Comments
 
LVL 1

Expert Comment

by:Bridge
Comment Utility
CMenu* pMenu = m_pMainWnd->GetMenu();
pMenu->EnableMenuItem( ... )
0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
I have written a program that accepts a double click and is then supposed to disable a menu item.. but the code you gave me produces an assertion error..

any further suggestions..
here is the code below.



void CSCVisTestView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
   
   CMenu* pMenu = GetMenu();
   pMenu->EnableMenuItem(ID_FILE_NEW,MF_GRAYED)
   
      CScrollView::OnLButtonDblClk(nFlags, point);
}

0
 
LVL 1

Expert Comment

by:Bridge
Comment Utility
Just looking at you code GetMenu probably returns NULL as the menu is associated with the frame. Put ASSERT(pMenu) on the next line to find out.
Try CMenu* pMenu = GetParentFrame()->GetMenu();
0
 
LVL 1

Expert Comment

by:Bridge
Comment Utility
Ignor what I've said. Talking bollocks
0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
It worked tho.. ish only for the top items.. not the sub menus.
code so far is:

CMenu* pMenu = GetParentFrame()->GetMenu();
pMenu->EnableMenuItem(ID_SMARTCARD_START,MF_GRAYED);
GetParentFrame()->DrawMenuBar();
 
0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
it didnt work for the ID_FILE_NEW tho.

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:Bridge
Comment Utility
CMenu* pMenu = GetMenu();
CMenu* pSubMenu = GetSubMenu(0);
pSubMenu->EnableMenuItem(ID_FILE_NEW,MF_GRAYED)
0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
I used this code.. but it didnt work.. No errors. but the new button wasnt greyed out..
(incase your wondering the ID_SMARTCARD_START) was a direct link of the menu bar and i was greyed out with the previous code)



   CMenu* pMenu =  GetParentFrame()->GetMenu();
   CMenu* pSubMenu = pMenu->GetSubMenu(0);
   pSubMenu->EnableMenuItem(ID_FILE_NEW,MF_GRAYED);
   GetParentFrame()->DrawMenuBar();

0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
I used this code.. but it didnt work.. No errors. but the new button wasnt greyed out..
(incase your wondering the ID_SMARTCARD_START) was a direct link of the menu bar and i was greyed out with the previous code)



   CMenu* pMenu =  GetParentFrame()->GetMenu();
   CMenu* pSubMenu = pMenu->GetSubMenu(0);
   pSubMenu->EnableMenuItem(ID_FILE_NEW,MF_GRAYED);
   GetParentFrame()->DrawMenuBar();

0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
Auto grade is soon.
0
 
LVL 2

Accepted Solution

by:
prasanth earned 50 total points
Comment Utility
I recommend you use the ON_UPDATE_COMMAND_UI MFC macro to do this. Select the view in ClassWizard, select the menu item (ID_FILE_NEW or whatever), and create a handle for ON_UPDATE_COMMAND_UI.

In your doubleclick handler function, keep the disbaled status in a BOOL variable.
Then in the ON_UPDATE_COMMAND_UI handler function you can simply call EnableItem with true or false depending on the state of the BOOL variable.
0
 
LVL 1

Author Comment

by:ramsay
Comment Utility
Great Worked like a charm!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This video discusses moving either the default database or any database to a new volume.

728 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

8 Experts available now in Live!

Get 1:1 Help Now