Maybe something like this would work for you...
public class MenuInfo
{
public short MenuId { get; set; }
public string MenuName { get; set; }
public string Url { get; set; }
public short ParentmenuId { get; set; }
public List<menuinfo> SubMenus {get; set;}
public MenuItem MakeMenuItem()
{
MenuItem item = new MenuItem();
return item;
}
}
Here's your recursive method:
private void BuildMenus(MenuInfo item, MainMenu menuControl, MenuItem menuItem)
{
MenuItem newItem = item.MakeMenuItem();
if (menuItem == null)
{
menuControl.MenuItems.Add(newItem);
}
else
{
menuItem.MenuItems.Add(newItem);
}
foreach (MenuInfo thisItem in item.SubMenus)
{
BuildMenus(thisItem, menuControl, newItem);
}
}
And the initial call:
public static void Example4()
{
using (Form form = new Form())
{
form.Menu = CreateMainMenu();
List<MenuInfo> list = CreateMenuInfos();
foreach (MenuInfo info in list)
{
BuildMenus(info, form.Menu, null);
}
form.ShowDialog();
}
}