How to add submenu items to menuitems
Asked Answered
H

4

9

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

https://static.mcmap.net/file/mcmap/ZG-Ab5ovKRkANC2AZV22bFljKmMva3/33ok3cw.jpg

Hitchhike answered 22/4, 2013 at 12:10 Comment(0)
S
32

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

Sting answered 22/4, 2013 at 12:16 Comment(2)
How to add click event to these submenu items?Hitchhike
@hs.chandra, you can add event through overloaded constructor.Sting
J
1
MenuItem newMenuItem1 = new MenuItem("Employee Master");
            MenuItem mnuSubMenuItem = new MenuItem();
            newMenuItem1.ChildItems.Add(mnuSubMenuItem);
            mnuSubMenuItem.Text = "Add User...";
            mnuSubMenuItem.NavigateUrl = "ADDURL.aspx";

            MenuItem mnuSubMenuItem1 = new MenuItem();
            newMenuItem1.ChildItems.Add(mnuSubMenuItem1);
            mnuSubMenuItem1.Text = "Edit User...";
            mnuSubMenuItem1.NavigateUrl = "EDITURL.aspx";
            this.MenuMaster.Items.Add(newMenuItem1);
Juggins answered 16/3, 2015 at 11:53 Comment(0)
K
0

You can add This

var someMenu= new MenuItem("Foo");
someMenu.MenuItems.Add( new MenuItem("foo "));
Karilla answered 22/4, 2013 at 12:18 Comment(0)
W
0

That format doesn't work for me. I'm forced to use:

// People often exclude the applicable "using" statements--so samples don't work!
using System.Web.UI.WebControls; 

// Create the MAIN menu item
MenuItem mnuMenuItem = new MenuItem();         

// Create the SUB menu item
MenuItem mnuSubMenuItem = new MenuItem();      

// Create the SUB menu item, "under" the MAIN menu item!
mnuMenuItem.ChildItems.Add(mnuSubMenuItem);  
Wofford answered 29/1, 2015 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.