Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Menu items

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello

Is it possible to enable and disable menu items during runtime? If so how?

Thank you
 
If the menu items you want disable/enable are the top-most menu items, then can use the EnableMenuItem member function of the CMenu class within CMainFrame

The function below disables/enables certain menu options according to global/member boolean variables.

void CMainFrame::SetMenuItems()
{

CMenu* pMenu = GetMenu();

if (m_bDisableDatabases)
{
pMenu->EnableMenuItem(TOPMENU_DATABASES, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
}
else
{
pMenu->EnableMenuItem(TOPMENU_DATABASES, MF_BYPOSITION | MF_ENABLED);
}

DrawMenuBar();

}

If the menus you want to disable/enable are sub-menus, then you can use Class Wizard against the menus resource, go to the Message Maps tab, select resource in the left-hand pane & the UPDATE_COMMAND_UI in the right-hand pane. Click on 'Add Function'. Inside the function you can do the following:-

void CMainFrame::OnUpdateMySubMenu(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE); // or FALSE to disable it !
}


Spencer Window (not a joke name)
 
Hi Spencer,

For the above lines(for using EnableMenuItem) is it not required to set the CMainFrame member variable m_bAutoMenuEnable to false(m_bAutoMenuEnable = FALSE;)
Correct me if I am wrong.

Regards
vrvijayaraj.
 
Thanks for the above.

The problem is that I need to enable and disable the menu item when the user presses a button. This is done in the view class. Do you have any ideas on how to do that?
 
Hi Guest,
First set m_bAutoMenuEnable = FALSE;
It is a member variable of CMainFrame.
After that try the following code from your view class as a handler of your button click. Hope it will work.
For additional help see help on EnableMenuItem and its flags from MSDN.
void CTestForMenuView::OnButton1()
{
CMenu* pMenu = GetParent()->GetMenu();
CMenu* submenu = pMenu->GetSubMenu(0);

submenu->EnableMenuItem(ID_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

GetParent()->DrawMenuBar();
}

Regards
vrvijayaraj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top