ContextMenuStrip and submenus - virtual impossibility??? huh?

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

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
 
Ron said:
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?

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
 
Back
Top