Search code examples
c#winformscontextmenutablelayoutmenu-items

How to add submenu items to menuitems


I have created a Table layout in windows forms as shown in figure, i have added a right mouse button click Menu to my table,

i wnt to add submenu items to following menuitems such as

When i click add device it should show subitems such as sounder, MCP, strobe etc When i click fault it should show subitems such as open circuit fault, sounder fault, `short circuit fault' so on

This is how i created menuitems

    ContextMenu m = new ContextMenu();
    MenuItem addDevice = new MenuItem("Add Device");
    MenuItem deleteDevice = new MenuItem("delete Device");
    MenuItem fire = new MenuItem("Fire");
    MenuItem fault = new MenuItem("Fault");
    MenuItem suppress = new MenuItem("Suppress");
    m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault,suppress});
    tableLayout.ContextMenu = m;
    m.Show((Control)(sender), e.Location);

The below is my form and table layout

http://i48.tinypic.com/33ok3cw.jpg


Solution

  • You can add MenuItems to existing MenuItem like:

    MenuItem addDevice = new MenuItem("Add Device");
    addDevice.MenuItems.Add( new MenuItem("Add More .."));
    

    It would be visible like:

    enter image description here