如何将子菜单项添加到菜单项

时间:2013-04-22 12:10:38

标签: c# winforms contextmenu tablelayout menu-items

我在Windows窗体中创建了一个Table布局,如图所示,我添加了一个鼠标右键单击Menu到我的表,

我想将子菜单项添加到以下菜单项中,例如

当我点击add device时,它应显示子项,例如sounderMCPstrobe等 当我点击fault时,它应显示子项,例如open circuit faultsounder fault,“短路故障”等等

这就是我创建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);

以下是我的表格和表格布局

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

4 个答案:

答案 0 :(得分:24)

您可以将MenuItems添加到现有MenuItem,例如:

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

如下所示:

enter image description here

答案 1 :(得分: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);

答案 2 :(得分:0)

您可以添加此

var someMenu= new MenuItem("Foo");
someMenu.MenuItems.Add( new MenuItem("foo "));

答案 3 :(得分:0)

这种格式对我不起作用。我被迫使用:

// 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);