我有一个winform,菜单栏如下:
File==>Add==>New Project
File==>Add==>Existing Project
File==>Open
File==>Exit
Edit==>Copy
Edit==>Cut
Help==>View Help
Help==>About
我想获取所有菜单项和子菜单项的文本 我试过这段代码:
for (int i = 0; i < menuStrip1.Items.Count; i++)
{
foreach (ToolStripMenuItem item in ((ToolStripMenuItem)menuStrip1.Items[i]).DropDownItems)
{
textBox1.Text += item.OwnerItem.Text + @"==>" + item.Text + Environment.NewLine;
}
}
结果是:
File==>Add
File==>Open
File==>Exit
Edit==>Copy
Edit==>Cut
Help==>View Help
Help==>About
因为它显示它没有显示所有子菜单项。我试过这段代码:
for (int i = 0; i < menuStrip1.Items.Count; i++)
{
for (int j = 0; j < ((ToolStripMenuItem) menuStrip1.Items[i]).DropDownItems.Count; j++)
{
foreach (ToolStripMenuItem item in (ToolStripMenuItem)menuStrip1.Items[i]).DropDownItems[j]))
{
textBox1.Text += item.OwnerItem.Text + @"==>" + item.Text + Environment.NewLine;
}
}
}
但它会出现此错误:
foreach statement cannot operate on variables of type 'System.Windows.Forms.ToolStripItem' because 'System.Windows.Forms.ToolStripItem' does not contain a public definition for 'GetEnumerator'
注意:我正在寻找一个通用的解决方案(对于任意数量的菜单项和任意数量的嵌套子菜单项),不仅仅是针对这个问题。
提前谢谢。
答案 0 :(得分:0)
使用recursion。
编写一个调用自身的方法(但也检查终止,以便在不需要深入时不调用自身。)
以下是一些代码:
static string GetText2(ToolStripMenuItem c)
{
string s = c.OwnerItem.Text + @"==>" + c.Text + Environment.NewLine;
foreach (ToolStripMenuItem c2 in c.DropDownItems)
{
s += GetText2(c2);
}
return s;
}
答案 1 :(得分:0)
您可以使用以下常规树遍历功能,取自How to flatten tree via LINQ?
public static class TreeHelper
{
public static IEnumerable<T> Expand<T>(this IEnumerable<T> source,
Func<T, IEnumerable<T>> elementSelector)
{
var stack = new Stack<IEnumerator<T>>();
var e = source.GetEnumerator();
try
{
while (true)
{
while (e.MoveNext())
{
var item = e.Current;
yield return item;
var elements = elementSelector(item);
if (elements == null) continue;
stack.Push(e);
e = elements.GetEnumerator();
}
if (stack.Count == 0) break;
e.Dispose();
e = stack.Pop();
}
}
finally
{
e.Dispose();
while (stack.Count != 0) stack.Pop().Dispose();
}
}
}
使用该功能和LINQ,获取所有菜单项很简单
var allMenuItems = menuStrip1.Items.OfType<ToolStripMenuItem>()
.Expand(item => item.DropDownItems.OfType<ToolStripMenuItem>());
并生成类似于示例的文本
textBox1.Text = string.Join(Environment.NewLine, allMenuItems
.Select(item => item.OwnerItem.Text + @"==>" + item.Text));