ArgumentException when adding a MenuItem

  • Thread starter Thread starter INCA Hellas Ltd
  • Start date Start date
I

INCA Hellas Ltd

Hello,
I am trying to fill a MenuItem of my MainMenu, with the contents of a
ContextMenu which is on another form.
This is supposed to happen when the second form is shown on a panel of the
first.
Also the MenuItem of the MainMenu (called ModMenu) is removed and added as
needed.

So I wrote the following method, however I am getting an argument exception
when I am adding the MenuItem.

public static bool ShowModMenu(Form1 f1,ContextMenu m)

{

f1.Menu.MenuItems.Add(f1.ModMenu);

Menu.MenuItemCollection f1i=f1.ModMenu.MenuItems;

f1i.Clear();

while (m.MenuItems.Count>0)

{

MenuItem mi=m.MenuItems[0];

m.MenuItems.Remove(mi); // I remove the menuitem from the context menu
because the docs say that a menuitem cannot be in two menus at the same time

f1i.Add(mi); // This throws ArgumentException

}

}

Can anybody point out where the problem is?

Thank you in advance,
Konstantinos Tragas
INCA Hellas Ltd.
 
During debugging I noticed the following:

A MenuItem just constructed has its Parent property set to null.
When it is added to a menu this menu becomes the MenuItem's Parent.
But when it is removed from the menu the Parent does not change back to
null, as I would expect, and I think that this explains the
ArgumentException when I try to add it to another menu.

This seems like a bug to me. Can anybody confirm this?

Thank you
K. Tragas
 
Back
Top