Problem description

When two objects of the MainMenu class having menu items with the MergeType=MergeItems and with the identical MergeOrder are merged using the MergeMenu method, the Popup event handler of the target menu item is getting lost.

The following example illustrates the problem.

For example, if both top level menu items menuItem1 of the mainMenu1 and menuItem2 of the mainMenu2 have the MergeType=MergeItems and defined handlers for the Popup event, then the following code will produce an error:

mainMenu1.MergeMenu(mainMenu2);

After executing this line the Popup event handler of the menuItem1 will be lost and the merged menu item will contain only the handler for the Popup event of the menuItem2. As any event is a multicast delegate, this behavior can be considered as a bug.

To resolve this problem, add an event handler for the Popup event of the menuItem1 again.

menuItem1.Popup += new EventHandler(menuItem1_Popup);

After executing this line, the Popup event will contain both Popup event handlers of the menuItem1 and the menuItem2.

IMPORTANT

If you have menu items of custom classes derived from the MenuItem class, make sure to have a default parameterless constructor in your menu class. The MergeMenu method internally calls CloneMenu method. The CloneMenu method dynamically creates objects of the menu classes, invoking a default parameterless constructor. If your custom menu class has no parameterless constructor, the MergeMenu method will fail with a cryptic system error message “No parameterless constructor defined for this object“.