By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,962 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,962 IT Pros & Developers. It's quick & easy.

ContextMenuStrip and submenus - virtual impossibility??? huh?

P: n/a
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
Oct 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ron M. Newman wrote:
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

Oct 31 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.