c#下怎样获得contextmenustrip动态添加的二级菜单项的文本

2024-12-13 20:42:12
推荐回答(2个)
回答1:

ToolStripMenuItem mi0 = (contextMenuStrip1.Items[0] as ToolStripMenuItem); //mi0是一级菜单项
if (mi0.HasDropDownItems) //如果mi0有子项
{
foreach (ToolStripMenuItem item in mi0.DropDownItems)
{
//item. //二级菜单项
}
}

回答2:

为动态创建的二级菜单绑定 mousedown事件, 在 mousedown事件中,写如下代码

if (e.Button == System.Windows.Forms.MouseButtons.Right) {
MessageBox.Show((sender as ToolStripMenuItem).Text);
}