How to add sub menu items in contextmenustrip using C#4.0?

心不动则不痛 提交于 2019-11-29 02:57:06

To add an item, you would call

myContextMenuStrip.Items.Add("Item title", null, myClickHandler);

To add a sub-menu, you take an existing item and do the same to it:

(myContextMenuStrip.Items[0] as ToolStripMenuItem).DropDownItems.Add(...)

Define your menu strip and items

ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem item, submenu;

Create new item.

submenu = new ToolStripMenuItem();
submenu.Text = "Sub-menu 1";

At this time your new item is just like others. Now create several new items and add them to submenu:

item = new ToolStripMenuItem();
item.Text = "Sub-item 1";
submenu.DropDownItems.Add(item);

item = new ToolStripMenuItem();
item.Text = "Sub-item 2";
submenu.DropDownItems.Add(item);

At last add sub-menu to your main ContextMenuStrip

menu.Items.Add(submenu);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!