Click to See Complete Forum and Search --> : All menuitems count?


paxromana
August 25th, 2004, 02:13 PM
hello

is there any way i can get the count of all the menuitems in the mainmenu withouth using recursion is there a property i havent seen or anythin else?

thanks

MRutledge
August 25th, 2004, 03:13 PM
For main menu items
MainMenu1.MenuItems.Count

For items in a paricular menu
MainMenu1.MenuItems[0].MenuItems.Count

paxromana
August 25th, 2004, 03:28 PM
thanks but im talking about counting all the menuitems even the submenuitems and every menuitem menuitems

i can do it with recursion but i wanna know if theres an easyer way

here is the code

private int iMenuItemCount = 0;

private int GetAllMenuItemsCount(Menu comunalMenu)
{
foreach(menuitems menuItem in comunalMenu.MenuItems)
{
iMenuItemCount++;
if(menuItem.IsParent)
{
iMenuItemCount+=menuItem.MenuItems.Count;
this.GetMenuItemsCount(menuItem);
}
else
return iMenuItemCount;
}
return iMenuItemCount;
}

private void GetMenuItemsCount(Menu comunalMenu)
{
foreach(menuitems menuItem in comunalMenu.MenuItems)
{
if(menuItem.IsParent)
{
iMenuItemCount+=menuItem.MenuItems.Count;
this.GetMenuItemsCount(menuItem);
}
}
return;
}

pareshgh
August 25th, 2004, 04:30 PM
if you know how many levels of menus you are already going to have then it would be possible without recursion, running 2-3 loops (depending upon nested menus)

otherwise your recursive solution seems okay ..

paxromana
August 26th, 2004, 10:56 AM
i made a better recursion method after 1 day :)

private int iMenuItemCount = 0;
private int GetMenuItemCount(Menu comunalMenu)
{
try
{
foreach(menuitems comunalItem in comunalMenu.MenuItems)
{
iMenuItemCount++;
if(comunalItem.IsParent)
this.GetMenuItemCount(comunalItem);
}

return iMenuItemCount;

}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}