Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

Im trying to create a dynamic menu by code (I'm reading the menu items from a SQL database).
But the problem is that I'm not able to create a sub menu.
For example I can create a menu titled FILE and a menuItem under it titeld NEW but I can't add a menuitem (PROJECT)under the menuItem NEW.

            MainMenu MMenu = new MainMenu();
            DataTable DT_Menu = GetDataTable();

            foreach (DataRow DR in DT_Menu.Rows)
            {               
                if (Convert.ToBoolean(DR["M_Header"]) == true) 
                {
                    MenuItem MItem = new MenuItem();
                    MItem.Name = DR["M_Name"].ToString();
                    MItem.Text = DR["M_Text"].ToString();
                    MMenu.MenuItems.Add(MItem);                    
                }
                else
                {                    
                        MenuItem CMItem = new MenuItem();
                        CMItem.Name = DR["M_Name"].ToString();
                        CMItem.Text = DR["M_Text"].ToString();

                        MenuItem ExistMnu = new MenuItem();
                        ExistMnu = (MenuItem) MMenu.MenuItems[DR["M_Parent"].ToString()];
                        if (ExistMnu != null)
                        {
                            ExistMnu.MenuItems.Add(CMItem);
                        }                    
                }
            }
            Menu = MMenu;
        }

        public DataTable GetDataTable()
        {
            DataTable DT = new DataTable("MENU");

            DataColumn Pmry2 = new DataColumn("M_Parent", typeof(string));
            Pmry2.Unique = false;
            DataColumn Pmry3 = new DataColumn("M_Name", typeof(string));
            Pmry3.Unique = true;

            DT.Columns.Add(Pmry2);
            DT.Columns.Add(Pmry3);
            DT.Columns.Add("M_Text", typeof(string));
            DT.Columns.Add("M_Header", typeof(bool));

            DataColumn[] myPrimaryKeyColumns = new DataColumn[2];
            myPrimaryKeyColumns[1] = DT.Columns["M_Name"];            
            DT.PrimaryKey = myPrimaryKeyColumns;

            DT.Rows.Add("" , "File", "FILE", true);
            DT.Rows.Add("File", "New", "NEW", false);            
            DT.Rows.Add("New", "Project", "PROJECT", false);            

            return DT; 
        }
    }
}


Thanks in advance.
Posted
Updated 4-Apr-11 10:39am
v2
Comments
Dalek Dave 4-Apr-11 16:39pm    
Edited for Grammar and Readability.

You're almost there. MenuItem has the property MenuItems (as well as Menu). You only need to use MenuItem.MenuItems.Add(existingMenuItem).

—SA
 
Share this answer
 
Comments
Manfred Rudolf Bihy 14-Mar-11 20:24pm    
Ahh the simplicity of a good answer in the early morning hours. 5+
(Early here in Germany, it's now 1:30 AM and I'm getting ready to hit the sack. See you later, SA.)
Sergey Alexandrovich Kryukov 14-Mar-11 20:26pm    
Yep. Thank you, Manfred.
--SA
Monjurul Habib 15-Mar-11 3:36am    
fantastic
Sergey Alexandrovich Kryukov 15-Mar-11 13:02pm    
Rather simple.
Thank you, Monjurul.
--SA
Rojeh 15-Mar-11 6:29am    
Thanks for your fast reply
but i think i did add the item menu!!!

ExistMnu.MenuItems.Add(CMItem)
Try this.
MainMenu MMenu = new MainMenu();
     DataTable DT_Menu = GetDataTable();
     MenuItem MItem = new MenuItem();

     foreach (DataRow DR in DT_Menu.Rows)
     {
         if (Convert.ToBoolean(DR["M_Header"]) == true)
         {
             MItem = new MenuItem();
             MItem.Name = DR["M_Name"].ToString();
             MItem.Text = DR["M_Text"].ToString();
             MMenu.MenuItems.Add(MItem);
         }
         else
         {
             MenuItem MItemChild = new MenuItem();

             MItemChild.Name = DR["M_Name"].ToString();
             MItemChild.Text = DR["M_Text"].ToString();
             MMenu.MenuItems.Add(MItemChild);

         }
     }
     Menu = MMenu;
 }
 
Share this answer
 
Comments
Rojeh 15-Mar-11 5:37am    
it appers as: FILE | NEW | PROJECT

and not:
FILE|
NEW | PROJECT
Toniyo Jackson 15-Mar-11 5:44am    
Did u understand the concept?
First i declared the object in 3rd line. Then in 7th line again creating new object(MItem) and adding that to menu. Then in else part creating child menu item and adding that to already last created MItem using the line "MMenu.MenuItems.Add(MItemChild);".
Rojeh 15-Mar-11 6:47am    
i understood the concept and tried it and it is not working fine,
i want to have the "File" and under it "New" and a sub menu of "New" named Project"
in the Else we are linking MItemChild ("new") directly to MMenu and so it is not appearing as a child of ("File")!! where are we linking "New" as a child to "File"??
Thanks for your fast reply :)
but i think i did add the item menu!!!
ExistMnu.MenuItems.Add(CMItem)
 
Share this answer
 
@ Toniyo: it created a flat menu and no sub menu!!
it appers as: FILE | NEW | PROJECT

and not:
FILE|
NEW | PROJECT

@ SAKryukov: still didn't know what is the solution u suggested to me

Thanks all 4 ur time
 
Share this answer
 
I found a good solution and it may help

Dynamic MainMenu formation in WinForm Application using DataSet[^]
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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