Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there,
I'm filling a menu recursively....But I dont get the value and Text in MenuItem "Item
How to resolve this? It is the sample code...it s not complete
C#
protected void FillMenuItems()
    {
        DataSet ds = objMenu.MainMenu();
        
        for (int i = 0; i<ds.Tables[0].Rows.Count  ;i++ )
        {
            MenuItem Item = new MenuItem();
            string MainMenuItem = ds.Tables[0].Rows[i]["mnuText"].ToString();
            string MainMenuValue = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
            Item.ChildItems.Add(new MenuItem(MainMenuItem, MainMenuValue));
            Menu1.Items.Add(new MenuItem(MainMenuItem, MainMenuValue));
            FillSubMenuItems(MainMenuValue, Item);
         }
      }

    protected void FillSubMenuItems(string ParentId,MenuItem ParentMenu)
    {
        DataSet ds = objMenu.SubMenu(ParentId);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
             MenuItem SubItem = new MenuItem();

             string SubItemText  = ds.Tables[0].Rows[i]["mnuText"].ToString();
             string SubItemValue  = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
             SubItem.ChildItems.Add(new MenuItem(SubItemText, SubItemValue));
             ParentMenu.ChildItems.Add(SubItem);
             FillSubMenuItems(SubItemValue,ParentMenu);
          //   Menu1.Items.Add(ParentMenu);
         }
        
    }


Thanks in advance
Posted
Updated 14-Mar-11 1:32am
v3

Erhm, I think if you modify the code like so :

protected void FillMenuItems()
    {
        DataSet ds = objMenu.MainMenu();
        
        for (int i = 0; i<ds.Tables[0].Rows.Count  ;i++ )
        {
            string MainMenuItem = ds.Tables[0].Rows[i]["mnuText"].ToString();
            string MainMenuValue = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
            Item.ChildItems.Add();
            MenuItem mainItem MenuItem(MainMenuItem, MainMenuValue)
            Menu1.Items.Add(mainItem);
            FillSubMenuItems(MainMenuValue, mainItem);
         }
      }
    protected void FillSubMenuItems(string ParentId,MenuItem ParentMenu)
    {
        DataSet ds = objMenu.SubMenu(ParentId);
        for (int i = 0; i < ds.Tables[0].Select("mnuParentId = " + ParentId); i++)
        {
             string SubItemText  = ds.Tables[0].Rows[i]["mnuText"].ToString();
             string SubItemValue  = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
             MenuItem subItem = new MenuItem(SubItemText, SubItemValue);
             ParentMenu.ChildItems.Add(subItem);
             FillSubMenuItems(SubItemValue,subItem);
          //   Menu1.Items.Add(ParentMenu);
         }
        
    }


You may have more luck.

Note : Check the table column names i've used because i'm not sure if the names are correct since I don't know the structure of your data source.
 
Share this answer
 
The MenuItem constructor doesn't have an overload that accepts two strings - I think you need to review the overloads for the MenuItem class:

http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.aspx[^]

Beyond that, you probably need to have more info in the database. I would probably have the following schema:

int ID (unique identifier)
int ParentID
string Name
int Sequence
string Tooltip

This way, you can freely add menu items in any order, associate one or more with a given Parent menu item's ID, and cause them to appear in any desired order. So You could have this:

ID  PARENT  NAME  SEQ  TOOLTIP
1     0     File   0   ""
2     1     Open   0   "Open an existing file"
3     1     Save   1   "Save the currently open file"
4     1     Exit   2   "Exit program"
5     0     Edit   1   ""
6     5     Copy   0   "Copy selected  items to clipboard"


At this point you could create your menu in a more organized fashion in your C# code. A parentID of 0 would indicate a root-level menu item, and you could add additional columns that defined whether or not the item is checked, enabled, or is a divider.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900