• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

CMenu::RemoveMenu is failing

Hi,

I am using RemoveMenu() to remove ALL menu items in my sub menus, however it does not delete ALL the items. It fails to remove a couple of items.
I cannot figure out why it is not deleting all the items
I have tried using GetLastError() however this only returns 0.

Any help would be much appreciated !
Thanks,
Danny


void myModule::RemoveSubMenuItems( CMenu* &pSubMenu)
{
  // Remove All menu items from the sub menu.
   int count = pSubMenu->GetMenuItemCount();

   for (int i = 0; i < count; i++)
   {
    if (!pSubMenu->RemoveMenu(i, MF_BYPOSITION))
    {
 
  CHAR szBuf[80];
  DWORD dw = GetLastError();
 
  sprintf(szBuf, "FAILED to RemoveMenu item: GetLastError returned %u\n", dw);
 
  AfxMessageBox(szBuf);
    }

   }
}
0
Danny01
Asked:
Danny01
1 Solution
 
AlexFMCommented:
for (int i = count - 1; i >=0; i--)
{
    pSubMenu->RemoveMenu(i, MF_BYPOSITION);
}

When you remove menu items from the start, numbers of all other items are changed.

0
 
Danny01Author Commented:
Thank you , this worked ... You have saved me lots of time and frustration !!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now