出于某种原因,无论tooltripmenuitem添加到上下文菜单的次数多少,它总是说上下文菜单不包含该项. ToolStripMenuItem Colour = new ToolStripMenuItem("Colour");ctmFile.Show(Cursor.Position);Selecteditem = lvFiles.Foc
ToolStripMenuItem Colour = new ToolStripMenuItem("Colour"); ctmFile.Show(Cursor.Position); Selecteditem = lvFiles.FocusedItem.Text.ToString(); if (lvFiles.FocusedItem.ImageKey.ToString() == "Folder") { if (ctmFile.Items.Contains(Colour) == false) { ctmFile.Items.Add(Colour); } } else { if(ctmFile.Items.Contains(Colour)) { ctmFile.Items.Remove(Colour); } }只是为了补充Ed的答案,我建议使用键代替:
ctmFile.Show(Cursor.Position); Selecteditem = lvFiles.FocusedItem.Text.ToString(); if (lvFiles.FocusedItem.ImageKey.ToString() == "Folder") { if (!ctmFile.Items.ContainsKey("Colour")) { ToolStripMenuItem Colour = new ToolStripMenuItem("Colour"); Colour.Name= "Colour"; //Add a name (key) to your menu. ctmFile.Items.Add(Colour); } } else { if (ctmFile.Items.ContainsKey("Colour")) { ctmFile.Items.RemoveByKey("Colour"); } }