I have a winform with menu bar like this :
File==>Add==>New Project
File==>Add==>Existing Project
File==>Open
File==>Exit
Edit==>Copy
Edit==>Cut
Help==>View Help
Help==>About
I want to get the text of all menu items and submenu items.
I have tried this code :
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;
}
}
and it results to :
File==>Add
File==>Open
File==>Exit
Edit==>Copy
Edit==>Cut
Help==>View Help
Help==>About
as it shows it does not show all of submenu items. I have tried this code :
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;
}
}
}
but it gives this error :
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'
Note : I am looking for a general solution(for the arbitrary number of menu item and arbitrary number of nested sub menu items) not only for this problem.
Thanks in advance.