如何在C#MDI表单中获取二级和三级菜单项 [英] How to get Second and Third level Menu Item in C# MDI Form
问题描述
我正在尝试访问MDI表单的Level2和Level 3菜单项,我该怎么办?
这是我的菜单结构>
I am trying to access the Level2 and Level 3 Menu Item of the MDI form how can I do that??
Here is my Menu Structure
MAIN MENU
SUB MENU -> Level 2 Menu1
SUB MENU 2 -> Level 2 Menu1 --> Level3 Menu1
我想获得子菜单的菜单文本 - > 2级菜单 - > 2级菜单1 - > 3级菜单
我试图遍历所有的MenuStrip菜单,ToolStripMenuItem和ToolStrip项目,但我只能获得菜单,直到SubMenu我无法达到Level2或Level2菜单。以下是我使用过的代码
I want to get Menu text of Sub Menu -> Level 2 Menu -> Level 2 Menu1 -> Level 3 Menu
I have tried to iterate through all the MenuStrip menu, ToolStripMenuItem and ToolStrip Items but I can only get the menus till SubMenu I cant reach till Level2 or Level2 Menu. Below is the code I have used
foreach (Control ctrl in coll)
{
MenuStrip menustrip = ctrl as MenuStrip;
if (ctrl.Text == "MenuStrip")
{
foreach (ToolStripMenuItem toolstripmenuitem in menustrip.Items)
{
foreach (ToolStripItem toolstripmenuitem in toolscriptmenuitem.DropDownItems)
{
MessageBox.Show(toolstripmenuitem.Text);
}
}
}
}
我正在使用C#Windows表单应用程序MDI表格。
请帮忙。
谢谢Ankit Roy
I am using C# Windows Form Application MDI Form.
Please help.
Thanks Ankit Roy
推荐答案
你需要在这里使用递归:
You need to use recursion here:
public void GetMenuItems(ToolStripItemCollection menuItems)
{
foreach (ToolStripMenuItem itm in menuItems)
{
Console.WriteLine(itm.Text);
if (itm.HasDropDown)
{
GetMenuItems(itm.DropDownItems);
}
}
}
这篇关于如何在C#MDI表单中获取二级和三级菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!