How to add a submenu item in submenu of Context menu

3.2k views Asked by At

I have a context menu that has some items like A, B C, D and E. Now D and E also has a sub menu items.

Ex D has I and J

E has K and L

and same K and L has some sub menu items. For example:

K has M

L has T

So my context menu will look like this..

A   
B    
C   
D --> I    
      J    
E --> K --> M   
      L --> T

So my question is how to add M and T items in toolstripitems which are K and L.

3

There are 3 answers

0
Yurii On

Here is sample code that creates menu like A --> B --> C:

ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem menuItemA = new ToolStripMenuItem("A");
menu.Items.Add(menuItemA);
ToolStripMenuItem menuItemB = new ToolStripMenuItem("B");
menuItemA.DropDownItems.Add(menuItemB);
ToolStripMenuItem menuItemC = new ToolStripMenuItem("C");
menuItemB.DropDownItems.Add(menuItemC);
this.ContextMenuStrip = menu;

If items you deal with ToolStripItem you will have to cast it to ToolStripMenuItem.

0
Umar T. On

I think that should be very simple to do using Windows Form Designer with the following steps:

Click on MenuItem E and Add MenuItem K just by typing 'K' in the text field shown by the designer (saying 'Type Here')

Now Click on MenuItem K and Add MenuItem M just by typing 'M'

Hope that will help.

0
Vignesh Kumar A On

You can add sub menu by using code behind

example

(myContextMenuStrip.Items["Item Status"] as ToolStripMenuItem).DropDownItems.Add("Submenu1", null, new EventHandler(gridcontexsubMenu_ItemClicked));

and refer the following link for context menu handler

http://msdn.microsoft.com/en-us/library/bb776881.aspx