I have a context menu strip. I can Add elements to its "Items", but there is
nothing in there to add a sub context menu strip.
Menu
Item 1
Item 2
Sub Item 1 <-- impossible to add!
How do I achieve that? There seems no API's to do that and the internet is
simply "empty" when it comes to that. All newsgroups questions are
unanswered. Does. Are we witnessing the death of the sub-menu here?
Ron
Bill B - 30 Oct 2006 20:49 GMT
Are you using the VS 2005 designer? When you click on a menu item (at
the top level - i.e. Item 2), you should see a submenu item with "Type
Here" as the text.
If you want to do this programatically, you can via the
.MenuItems.Add() method of of the top level menu. See the link below
for more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbts
kAddingContextMenusToForm.asp
Bill
Chris Dunaway - 31 Oct 2006 16:53 GMT
> I have a context menu strip. I can Add elements to its "Items", but there is
> nothing in there to add a sub context menu strip.
[quoted text clipped - 7 lines]
> simply "empty" when it comes to that. All newsgroups questions are
> unanswered. Does. Are we witnessing the death of the sub-menu here?
In order to have sub items, you need to use a ToolStripMenuItem class
to do it:
private void button1_Click(object sender, EventArgs e)
{
ContextMenuStrip cs = new ContextMenuStrip();
cs.Items.Add("Menu Item 1");
cs.Items.Add("Menu Item 2");
//This is the item we want to have sub items, so we need to create
it manually
//so that we can add items to it later
ToolStripMenuItem tm = new ToolStripMenuItem("Menu Item with Sub
Items");
cs.Items.Add(tm);
//Here we add sub items to the ToolStripMenuItem that we added
earlier
tm.DropDownItems.Add("Sub Item 1");
tm.DropDownItems.Add("Sub Item 2");
ToolStripMenuItem tm1 = new ToolStripMenuItem("More Sub Items");
tm.DropDownItems.Add(tm1);
tm1.DropDownItems.Add("Sub Sub Item 1");
tm1.DropDownItems.Add("Sub Sub Item 2");
//Set the buttons ContextMenuStrip property to the context menu we
just created.
button1.ContextMenuStrip = cs;
}
Incidentally, if you drag a ContextMenuStrip to the form, you can
design the menu visually and then look at the generated code to see how
it is done.
Chris