Search code examples
c#c#-4.0contextmenustrip

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


I have one contextmenustrip control associated with treenode. I have created one menu item in contextmenustrip manually in the form itself(for example named as "Assign").

Now I want to add sub menu items whenever user clicks this Assign item, it will create a list of users name as a sub menu item with checked or unchecked option.

For example, once user clicked Assign then I want to show the user name dynamically.


Solution

  • 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(...)