Search code examples
c#winformscontextmenustrip

Add ToolStripItem into ContextMenuStrip


So here is my problem. I want my context menu to open a ToolStrip. I have succeeded but the context menu will only open after the SECOND right click.

I have created a simple Form that shows the problem. It contain a menuStrip with some toolStripMenuItems, an empty contextMenuStrip and a button to test the right click. All of them created with the visual studio designer. Now here is the relevant code:

namespace Projet_test
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent(); //Contain this line: this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
    }

    //called by the Opening evenHandler of the context menu.
    private void contextOpening(object sender, CancelEventArgs e)
    {
      int j = 0;
      int total = menu1ToolStripMenuItem.DropDownItems.Count;
      for (int i = 0; i < total; i++)
      {
        contextMenuStrip1.Items.Insert(j, menu1ToolStripMenuItem.DropDownItems[0]);
        j++;
      }
    }

    //called by the Closed evenHandler of the context menu.
    private void contextClosed(object sender, ToolStripDropDownClosedEventArgs e)
    {
      int j = 0;
      int total = contextMenuStrip1.Items.Count;
      for (int i = 0; i < total; i++)
      {
        menu1ToolStripMenuItem.DropDownItems.Insert(j, contextMenuStrip1.Items[0]);
        j++;
      }
    }
  }
}

As you can see, right clicking on the button will show the context menu with the proper ToolStripMenuItems but only after the second click..(and empty the menuStrip on the first click because a toolStripMenuItem cannot be at two place at the same time).

Then closing the context menu will recreate the menuStrip properly.

I don't understand because while the context menu items are added dynamically, the context menu itself is initialized on the Form load.

So how can I make the context menu open on the first right click?


Solution

  • Your ContextMenuStrip is empty, which is why it isn't showing anything. Try adding a "dummy" item to trigger the menu to show itself:

    public Form1()
    {
      InitializeComponent();
      contextMenuStrip1.Items.Add("dummy");
    }
    

    Then remove it before you add your new items:

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
      contextMenuStrip1.Items[0].Dispose();
      int j = 0;
      int total = menu1ToolStripMenuItem.DropDownItems.Count;
      for (int i = 0; i < total; i++) {
        contextMenuStrip1.Items.Insert(j, enu1ToolStripMenuItem.DropDownItems[0]);
        j++;
      }      
    }
    

    And add it back in again when you close it so that the menu isn't empty again:

    private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e) {
      int j = 0;
      int total = contextMenuStrip1.Items.Count;
      for (int i = 0; i < total; i++) {
        menu1ToolStripMenuItem.DropDownItems.Insert(j, contextMenuStrip1.Items[0]);
        j++;
      }
      contextMenuStrip1.Items.Add("dummy");
    }