Creating the context menus consists of adding the ContextMenuBar control to the form and using the designer action items or right-click context menu commands to add items to it. The top level button that is added to the ContextMenuBar acts as the context menu parent that is assigned to the controls through the designer.

Once the ContextMenuBar control is added to the form and context menus created each control will have ContextMenuEx property in designer which you can expand and choose the context menu to assign it to the given control.

Please note that you can always examine the code in InitializeComponent method of the form with the ContextMenuBar control to see how the actual menu items are created and assigned to the controls.

Here is sample code which creates new Context-Menu and adds it to ContextMenuBar, then it assigns the context-menu to a TextBox control on the form:

VB:

' Create context menu item that is assigned to controls or items
Dim contextMenu As New ButtonItem("myContextMenuItemName")
' Create menu items
Dim menuItem As New ButtonItem("menuItemName1")
menuItem.Text = "Context Menu Item &1"
AddHandler menuItem.Click, AddressOf MenuItemClick
' Add item to Context Menu
contextMenu.SubItems.Add(menuItem)
 
' Create second context menu item
menuItem = New ButtonItem("menuItemName1")
menuItem.Text = "Context Menu Item &2"
AddHandler menuItem.Click, AddressOf MenuItemClick
' Add item to Context Menu
contextMenu.SubItems.Add(menuItem)
 
' Create third context menu item
menuItem = New ButtonItem("menuItemName1")
menuItem.Text = "Context Menu Item &3"
AddHandler menuItem.Click, AddressOf MenuItemClick
' Add item to Context Menu
contextMenu.SubItems.Add(menuItem)
 
' Add Context Menu to contextMenuBar1
contextMenuBar1.Items.Add(contextMenu)
 
' Assign context menu to text-box
contextMenuBar1.SetContextMenuEx(textBoxX1, contextMenu)

CS:

// Create context menu item that is assigned to controls or items
ButtonItem contextMenu = new ButtonItem("myContextMenuItemName");
// Create menu items
ButtonItem menuItem = new ButtonItem("menuItemName1");
menuItem.Text = "Context Menu Item &1";
menuItem.Click += new EventHandler(MenuItemClick);
// Add item to Context Menu
contextMenu.SubItems.Add(menuItem);
 
// Create second context menu item
menuItem = new ButtonItem("menuItemName1");
menuItem.Text = "Context Menu Item &2";
menuItem.Click += new EventHandler(MenuItemClick);
// Add item to Context Menu
contextMenu.SubItems.Add(menuItem);
 
// Create third context menu item
menuItem = new ButtonItem("menuItemName1");
menuItem.Text = "Context Menu Item &3";
menuItem.Click += new EventHandler(MenuItemClick);
// Add item to Context Menu
contextMenu.SubItems.Add(menuItem);
 
// Add Context Menu to contextMenuBar1
contextMenuBar1.Items.Add(contextMenu);
 
// Assign context menu to text-box
contextMenuBar1.SetContextMenuEx(textBoxX1, contextMenu);

To remove context menu from control you use following code:

VB:

' Remove context menu from text-box
contextMenuBar1.SetContextMenuEx(textBoxX1, Nothing)

CS:

// Remove context menu from text-box
contextMenuBar1.SetContextMenuEx(textBoxX1, null);

Related posts:

  1. How to Create Popup Menu or Toolbar Using Code
  2. How to Create Menu Using Code
  3. How to set ColorScheme for context menus
  4. How to Create Toolbars Using Code
  5. How to create Dockable Windows