MenuItems.Count

  • Thread starter Thread starter genc ymeri
  • Start date Start date
G

genc ymeri

Hi,
I'm a newbie in C# programming but something very weird is happening to me
:) :)

I have this menu in a C# form.

"File" and under this {"New","Open", "Exit"} and
"Help" with {"Content", "About"};

when I run this code :

for (short j = 0; (j < mnufrmMain.MenuItems.Count );j++)
{
MessageBox.Show(mnufrmMain.MenuItems[j].Text);
}

only "File" and "Help" show up while all the other menu items e.g.
{"New","Open", "Exit","Content", "About"}; do not show ?

Any explanation or how to browse in each of these items will be greatly
appreciated.

Thank You very much in advance !
 
The reason is New, Open, Exit are all under mnufrmMain.MenuItems[0].MenuItems. But your loop looks for onl
mnufrmMain.MenuItems. MenuItems form a tree like sructure

Add a second for loop within the first to iterate thru the sub menu items too, and ur problem will be solved

HTH
fbhcah
 
I see.....it's complicated b/c in run time we don't know how many nasted
sub-menus we have. It seems that with the mnufrmMain.MenuItems[0].MenuItems
method I can't loop through all of the sub-sub-sub-submenu items.

Is there any other way to browse all the menuitems created in a C# form at
the run time ?

Thanks a lot.

fbhcah said:
The reason is New, Open, Exit are all under
mnufrmMain.MenuItems[0].MenuItems. But your loop looks for only
mnufrmMain.MenuItems. MenuItems form a tree like sructure.

Add a second for loop within the first to iterate thru the sub menu items
too, and ur problem will be solved.
 
You could use recursive function to do what you're looking for, something
like you see below:

from whereever in your code, you can call:
ScanMenuItems(this.Menu); // or menufrmMain or whatever

then make sure you have ScanMenuItems defined, something like this:

public void ScanMenuItems(MenuItem menu)
{
for (short j = 0; (j < menu.MenuItems.Count );j++)
{
MessageBox.Show(menu.MenuItems[j].Text);
ScanMenuItems(menu);
}
}

public void ScanMenuItems(Menu menu)
{
for (short j = 0; j < menu.MenuItems.Count; j++)
{
MessageBox.Show(menu.MenuItems[j].Text);
ScanMenuItems(menu);
}
}

You'll need the overloaded definition if you want to start with a Menu
class, which is the top level menu definition for a main menu, and then
recurse into submenus from there down.

HTH

Ryan Gregg



genc ymeri said:
I see.....it's complicated b/c in run time we don't know how many nasted
sub-menus we have. It seems that with the mnufrmMain.MenuItems[0].MenuItems
method I can't loop through all of the sub-sub-sub-submenu items.

Is there any other way to browse all the menuitems created in a C# form at
the run time ?

Thanks a lot.

fbhcah said:
The reason is New, Open, Exit are all under
mnufrmMain.MenuItems[0].MenuItems. But your loop looks for only
mnufrmMain.MenuItems. MenuItems form a tree like sructure.

Add a second for loop within the first to iterate thru the sub menu
items
too, and ur problem will be solved.
HTH,
fbhcah
 
Back
Top