Link to home
Start Free TrialLog in
Avatar of pipe
pipe

asked on

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 );
Avatar of abancroft
abancroft

What's the return value from CheckMenuItem()?
Avatar of pipe

ASKER

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.
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!
Avatar of pipe

ASKER

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.

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
pipe:
   Since this is a popup menu, first get the position of the menu item and THEN check that menu position.
   Glenn
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!
Avatar of pipe

ASKER

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 );
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
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
Avatar of pipe

ASKER

***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.
ASKER CERTIFIED SOLUTION
Avatar of artpro
artpro

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pipe

ASKER

haha