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!

Disabling menu items

Status
Not open for further replies.

cag27

Programmer
Mar 1, 2001
9
US
I have scoured this FAQ and all documentation that I could find on this subject, tried several suggested changes, and I still cannot disable a selected menu item! I am working on an MFC AppWizard executable program using Visual Studio 6.0. Here is the code that I currently have:

In my App .h file:
afx_msg void SkipQuestion(CCmdUI* pCmdUI);

In my App .cpp file:
ON_UPDATE_COMMAND_UI(ID_SKIPQUESTION_SKIPQUESTION, SkipQuestion)

I have no ON_COMMAND message mapped to this ID.

In my App .cpp file:
void CQuestionApp::SkipQuestion(CCmdUI* pCmdUI)
{
int result;

//the following statement is on one line in my program
result = AfxMessageBox("Do you really want to skip
this question?", MB_YESNO);

if(result == IDYES)
{
pCmdUI->Enable(FALSE);

//Once again, the following is on one line
AfxMessageBox("You cannot skip any more questions
during this round.", MB_OK);
}
else //if(result == IDNO)
{
//One line
AfxMessageBox("The current question will not be
skipped.", MB_OK);
}
}

This code does display the AfxMessageBox following the 'result == IDYES' statement.

What I want is to do is disable the 'Skip Question' menu item if the user selects the 'Yes' button on the AfxMessageBox. I'm sure there is something obvious that I am overlooking here, so please let me know what the problem is! Thank you in advance.

Carrie
 

At first you need one handler for the action (open the AfxMessageBox and get the user choice) and one handler for the menu enabling/disabling operation.
Note that the menu enabling/disabling code is called by the MFC framework only when the menu is pulldown...

So on solution is to set up a handler for the display of the messagebox and a storage of result in a member of the class ( m_result) and a handler for the menu that test the value of the member m_result.

ON_COMMAND(ID_..., OnHandler)
ON_UPDATE_COMMAND_UI(ID_..., OnMenuHandler)

OnHandler()

m_result = AfxMessageBox("Do you really want to skip
this question?", MB_YESNO);

OnMenuHandler( CCmdUI* pCmdUI)

pCmdUI->Enable( m_result == IDYES)

Hope it helps

Thierry



 
Thank you so much for your help--once you pointed out what I had been doing wrong, everything fell into place and it now works perfectly.

Carrie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top