9

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

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

当我点击add device它应该显示子项,例如sounderMCPstrobe当我单击fault 它应该显示子项,例如open circuit fault,,sounder fault`短路故障'等等

这就是我创建菜单项的方式

    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

4

4 回答 4

30

您可以添加MenuItems到现有的MenuItem类似:

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

它会像这样可见:

在此处输入图像描述

于 2013-04-22T12:16:40.977 回答
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);
于 2015-03-16T11:53:56.373 回答
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);  
于 2015-01-29T14:57:50.567 回答
0

你可以添加这个

var someMenu= new MenuItem("Foo");
someMenu.MenuItems.Add( new MenuItem("foo "));
于 2013-04-22T12:18:15.823 回答