Question

I have one Toolstrip control inside that i am adding some ToolStripMenuItem. For every ToolStripMenuItem, I want one submenu (like Close). And click of right mouse button on any ToolStripMenuItem that submenu should show.

ToolStripMenuItem subMenuitem1 = new ToolStripMenuItem("Close");
items.DropDownItems.Add(subMenuitem1);

But this is not working. Please suggest me something. Thank You

Was it helpful?

Solution

What is "items" object?

Adding submenu:

// new menu, if you're using designer you should have it already
ContextMenuStrip mnu = new ContextMenuStrip();

// new tool strip item
ToolStripMenuItem mnuItem1 = new ToolStripMenuItem();
mnuItem1.Text = "Some text 1";
mnuItem1.Name = "mnuItem1";

// new submenu item
ToolStripMenuItem mnuItem2 = new ToolStripMenuItem();
mnuItem2.Text = "Some text 2";
mnuItem2.Name = "mnuItem2";

// connect them...
mnuItem1.DropDownItems.Add(mnuItem2);
mnu.Add(mnuItem1);

Now it's the matter of subscribing to events.

You want the submenu to appear on right mouse click. Normally it'll appear when mouse hovers over item that has a submenu. So if you're set on doing it your way, I suppose you'll have to add it manually when main toolStripItem is right clicked.

ToolStripMenuItem doesn't have MouseClick event. You can remember which mouse button was clicked in MouseDown event, then handle it in Click event.

Also you should remember that adding one instance of ToolStripMenuItem to one menu multiple times, or adding the same item to multiple menus, won't work. You'll have to create new item every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top