当前位置 : 主页 > 编程语言 > c语言 >

c# – 动态添加上下文菜单项

来源:互联网 收集:自由互联 发布时间:2021-06-25
出于某种原因,无论tooltripmenuitem添加到上下文菜单的次数多少,它总是说上下文菜单不包含该项. ToolStripMenuItem Colour = new ToolStripMenuItem("Colour");ctmFile.Show(Cursor.Position);Selecteditem = lvFiles.Foc
出于某种原因,无论tooltripmenuitem添加到上下文菜单的次数多少,它总是说上下文菜单不包含该项.

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");
    }
}
网友评论