C# 我知道如何添加右键快捷菜单 但如何动态添加里面的菜单项呢? 求解
你需要把ToolStripMenuItem强制转换为ToolStripDropDownItem类型才能添加子项,其实你自己看看Form1.Designer.cs里设计器自动生成的代码就会明白了,方法如下
private void button1_Click(object sender, EventArgs e)
{
((ToolStripDropDownItem)(((ToolStripDropDownItem)contextMenuStrip1.Items["aToolStripMenuItem"]).DropDownItems["eToolStripMenuItem"])).DropDownItems.Add(new ToolStripMenuItem("h"));
//或者
ToolStripDropDownItem ts = ((ToolStripDropDownItem)contextMenuStrip1.Items["aToolStripMenuItem"]);
ts = ((ToolStripDropDownItem)ts.DropDownItems["eToolStripMenuItem"]);
ts.DropDownItems.Add(new ToolStripMenuItem("h"));
}