Use Part of the Main Menu for a Context Menu

Problem

You want to create a context menu that shows the same entries as part of an application main menu.

Solution

Use the CloneMenu method to duplicate a portion of the main menu.

Discussion

In many applications, a control’s context-sensitive menu duplicates a portion of the main menu. Unlike Visual Basic 6, .NET differentiates between context menus and main menus, and a menu item can only belong to one menu at a time.

The solution is to make a duplicate copy of a portion of the menu using the CloneMenu method. The CloneMenu method not only copies the appropriate MenuItem items (and any contained submenus), it also registers the MenuItem with the same event handlers. Thus, when a user clicks a cloned menu ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.