我在Windows窗体中创建了一个Table布局,如图所示,我添加了一个鼠标右键单击Menu到我的表,
我想将子菜单项添加到以下菜单项中,例如
当我点击add device
时,它应显示子项,例如sounder
,MCP
,strobe
等
当我点击fault
时,它应显示子项,例如open circuit fault
,sounder 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
答案 0 :(得分:24)
您可以将MenuItems
添加到现有MenuItem
,例如:
MenuItem addDevice = new MenuItem("Add Device");
addDevice.MenuItems.Add( new MenuItem("Add More .."));
如下所示:
答案 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);