Help with CheckMenuItem

Hiya,
  Im using the code below to try and uncheck one menu item, then check another. However, the changes never take place. Any help would be great.

Thanks,
 jer


      // The graph menu
      CMenu mMenu;

      // Load the menu.
      mMenu.LoadMenu( IDR_GRAPHPOPUP );
 
      mMenu.CheckMenuItem( IDM_GRAPH_MODE_BAR, MF_BYCOMMAND | MF_UNCHECKED );
      mMenu.CheckMenuItem( IDM_GRAPH_MODE_LINE, MF_BYCOMMAND | MF_CHECKED );
pipeAsked:
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.

abancroftCommented:
What's the return value from CheckMenuItem()?
0
pipeAuthor Commented:
mMenu.CheckMenuItem( IDM_GRAPH_MODE_BAR, MF_BYCOMMAND | MF_UNCHECKED );


is returning an 8, which means the previous state of the item was MF_CHECKED.
0
artproCommented:
You can use ON_UPDATE_COMMAND_UI to check and uncheck all menu,like this :

void CMainFrame::OnUpdateRangeAll(CCmdUI* pCmdUI)
{
if (....)
   pCmdUI->SetCheck(1);
else
   pCmdUI->SetCheck(0);
}

MFC MainFrame can check and uncheck menu when it is idle. it works wvry well!
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

pipeAuthor Commented:
Arrg. I tried this and it didnt work either. The ON_UPDATE_COMMAND_UI triggers the correct function, and then  call the code with the CCmdUI object passed in but it doesnt work.

0
Vinayak KumbarSr Program ManagerCommented:
Hi,

Modify the code like
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);
menu.CheckMenuItem(ID_ONE, MF_BYCOMMAND | MF_CHECKED);
menu.CheckMenuItem(ID_TWO, MF_BYCOMMAND | MF_CHECKED);
AfxGetMainWnd()->SetMenu( &menu );
AfxGetMainWnd()->DrawMenuBar();
menu.Detach ();

Hope this solves Ur problem
VinExpert
0
GlennDeanCommented:
pipe:
   Since this is a popup menu, first get the position of the menu item and THEN check that menu position.
   Glenn
0
artproCommented:
Yes,MSDN say:

"A pop-up menu item must be checked by position since it does not have a menu-item identifier associated with it."

So use position to check it!
0
pipeAuthor Commented:
Yes, this is a popup menu, Im sorry for not mentioning that before. I changed my code to whats below, but still not luck. The return values do return the previous state correctly, so I'm completely out of ideas here.

      // The graph menu
      CMenu mMenu;

      // Load the menu.
      mMenu.LoadMenu( IDR_GRAPHPOPUP );
 
      CMenu *pmMenu = NULL;
      pmMenu = mMenu.GetSubMenu(0);
 
      pmMenu->CheckMenuItem( IDM_GRAPH_MODE_BAR, MF_BYCOMMAND | MF_UNCHECKED );
            
      pmMenu->CheckMenuItem( IDM_GRAPH_MODE_LINE, MF_BYCOMMAND | MF_CHECKED );
0
GlennDeanCommented:
But, you still aren't checking on position number.  For example, if the popup menu has 5 items, positions 0 thru 4, BUT IDM_GRAPH_MODE_BAR is something like 14005, then there's a definite problem.
   Glenn
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

Okay, no prob.
I will give u some steps, follow them, I assume there are two menu items Item1 and Item2 in the popup menu

In the context menu function U are going to popup the menu as
CMenu menu;
menu.LoadMenu(IDR_MENU1);
popup = menu.GetSubMenu(0);

popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,AfxGetMainWnd());

Maintain a variable say int m_MenuItemToBeChecked for that class. Default it to say 0 (corresponds to Item1).

Then in the command handlers for Item1 and Item2, set that variable to the required val(0 - Item1, 1 - Item2) as

void CSDIMenuToolView::OnItem1()
{      
      m_MenuItemToBeChecked= 0;
              //Do other things
}

void CSDIMenuToolView::OnItem2()
{
      m_MenuItemToBeChecked= 1;
              //Do other things
}


Then Map the ON_UPDATE_COMMAND_UI functions and do the following
void CSDIMenuToolView::OnUpdateItem1(CCmdUI* pCmdUI)
{
      if(m_MenuItemToBeChecked == 0)      
            pCmdUI->SetCheck(1);
      else
            pCmdUI->SetCheck(0);
}

void CSDIMenuToolView::OnUpdateItem2(CCmdUI* pCmdUI)
{
      if(m_MenuItemToBeChecked == 1)      
            pCmdUI->SetCheck(1);
      else
            pCmdUI->SetCheck(0);
}

That will give u the menu item checked, whichever U have already selected.

Try it out.
VinExpert
0
pipeAuthor Commented:
***BONK****  This is what happened. From what I see now; and it makes sense I guess.

Everytime you call trackpopup it has to make a new menu. I was ASSUMING that the check marks were retained between popups. They arent and that is logical because you have to rebuild the menu. If this was a main menu, the problem would never happen.  

Now that I look back at artpro's answer, I think I see what he was saying. RESET the check everytime you make it!!

Sorry, if you resubmit art, ill give you your points. Thanks everyone.
0
artproCommented:
Ha,I think i will be right!

thanks, pipe

artpro
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
pipeAuthor Commented:
haha
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.