VB6 and VB.NET deal with menu items in different ways. More specifically, VB6 allows you to dynamically create a submenu or a context menu immediately before displaying it, and to destroy it immediately afterwards. This feature allows VB6 developers to create dynamic menus based on control arrays, using the following sequence of actions:
- add elements to the menu control array by means of the array’s Load method
- display a context menu using the PopupMenu method
- destroy elements that were added in point 1, by means of the Unload method
This sequence works correctly because VB6 is able to process the Click event from an element of the menu array even if the element has been unloaded in the meantime.
Conversely, VB.NET requires that the menu elements exist at the instant when the Click event is processed. For this reason the above sequence of actions throws an exception after being migrated to VB.NET. You have to manually edit the code to abide to the following sequence:
- destroy any element that was added during a previous iteration, by means of the Unload method
- add elements to the menu control array, by means of the Load method
- display a context menu using the PopupMenu method
In other words, instead of destroying all menu items immediately after displaying the menu, you should add programming logic that destroy any old element and then rebuilds the menu immediately before displaying the popup menu.