Extend default context menu

Probably a very stupid question, but how can you extend a default context menu with your own menu items.

My question is: I want to have my own context menu, but in there I want to have the default Copy/Paste/Cut, ... functionality.

Thanks

Jim


Answer this question

Extend default context menu

  • Umair Khan

    something like the following perhaps

    void AddContextMenu()
    {
    if (ContextMenu != null)
    ContextMenu.Dispose();
    MenuItem[] menuItems = new MenuItem[3];
    menuItems[0] = new MenuItem("Full window");
    menuItems[1] = new MenuItem("Window set");
    menuItems[2] = new MenuItem("Window del");
    for (int i = 0; i < menuItems.Length; i++)
    menuItems[i].Click += new EventHandler(ContextMenu_Click);
    ContextMenu = new ContextMenu(menuItems);
    }
    private void OnMouseUp(object sender,MouseEventArgs e)
    {
    switch (e.Button)
    {
    case MouseButtons.Right:
    AddContextMenu();
    ContextMenu.Show(window,new Point(e.X,e.Y));
    break;
    }
    }

    mentioned <strong>ContextMenu</strong> seems to be property of a <strong>Control</strong> so can be hung under any Control.
    The code above I edited from code I have working, this Contextmenue pops up at the mousecursor when the right buttone is released. of course this is merly one of the possibilities, ContextMenu_Click handler is not shown here but must be implemented to get things working of course!

  • sjnaughton

    Well the help on the <strong>MergeMenu</strong> lists the following sample:

    private void MergeMyMenus()
    {
       // Set the merge type to merge the items from both top menu items.
       menuItem1.MergeType = MenuMerge.MergeItems;
       menuItem2.MergeType = MenuMerge.MergeItems;
       // Create a copy of my menu item.
       MenuItem tempMenuItem = new MenuItem();
       // Create a copy of menuItem1 before doing the merge.
       tempMenuItem = menuItem1.CloneMenu();
       // Merge menuItem1's copy with a clone of menuItem2
       tempMenuItem.MergeMenu(menuItem2.CloneMenu());

       // Add the merged menu to the ContextMenu control.
       contextMenu1.MenuItems.Add(tempMenuItem);
    }

    Somewhere on this forum some issues where discussed on this method, however to my knowledge the method works and the issues where of behavioural nature.
    I hope this helps.

    Ah, I see the contextmenu you speak of, as said ContextMenu is a property of Control (seems to be empty for buttons), so you should get it as <strong>textBox.ContextMenu</strong>

  • sharpej

    Thanks for the reply.  However, my question was not how to implement a custom context menu, but more, how to extend the default context menu of let's say a textbox.

    If you right click on a text box, a default context menu will popup with "Copy", "Paste", ... items.  I want to keep these, but also add my own context items.  How can I do this, without rewriting the "Copy", "Paste", ... menu items and their event handlers.

  • Extend default context menu