Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamic Menu

Dynamic Menu

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vatzcar
    wrote on last edited by
    #1

    i want to add Menu>Submenu dynamically, depending upon the data i receive. till now i did : // init. menu MainMenu mnuMain = new MainMenu(); // define menu MenuItem mnuFile = new MenuItem("&File"); MenuItem mnuMod = new MenuItem("&Module"); // add menu to main menu mnuMain.MenuItems.Add(mnuFile); mnuMain.MenuItems.Add(mnuMod); // define submenu MenuItem mnuFExit = new MenuItem("E&xit", new EventHandler(mnuFExit_Click),Shortcut.CtrlX); // add submenu to main menu mnuFile.MenuItems.Add(mnuFExit); foreach (string strKeyName in arrValue) // {arrValue contains some value} { mnuMod.MenuItems.Add(strKeyName,new EventHandler(mnuModMenu_Click)); } till here it's working perfectly. now i want to add submenu to those menu i'v added dynamically. only i have the text of those menu(strKeyName), but don't have any name so can't add menuitems referring them. how can i do that?

    E L 2 Replies Last reply
    0
    • V vatzcar

      i want to add Menu>Submenu dynamically, depending upon the data i receive. till now i did : // init. menu MainMenu mnuMain = new MainMenu(); // define menu MenuItem mnuFile = new MenuItem("&File"); MenuItem mnuMod = new MenuItem("&Module"); // add menu to main menu mnuMain.MenuItems.Add(mnuFile); mnuMain.MenuItems.Add(mnuMod); // define submenu MenuItem mnuFExit = new MenuItem("E&xit", new EventHandler(mnuFExit_Click),Shortcut.CtrlX); // add submenu to main menu mnuFile.MenuItems.Add(mnuFExit); foreach (string strKeyName in arrValue) // {arrValue contains some value} { mnuMod.MenuItems.Add(strKeyName,new EventHandler(mnuModMenu_Click)); } till here it's working perfectly. now i want to add submenu to those menu i'v added dynamically. only i have the text of those menu(strKeyName), but don't have any name so can't add menuitems referring them. how can i do that?

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      You could try something like:

      foreach (string strKeyName in arrValue)
      {
      mnuMod.MenuItems.Add(strKeyName, new EventHandler(mnuModMenu_click));
      foreach (MenuItem mi in mnuMod.MenuItems)
      {
      if (mi.Text == strKeyName)
      {
      //add submenus - mi.ChildItems.Add(/*parameters*/)
      }
      }
      }

      Obviously this assumes that the Text value of each menu item is unique. However, if you have duplicates in the menu it will confuse the user... so take it for what its worth. -- modified at 13:42 Friday 10th March, 2006

      V 1 Reply Last reply
      0
      • E eggsovereasy

        You could try something like:

        foreach (string strKeyName in arrValue)
        {
        mnuMod.MenuItems.Add(strKeyName, new EventHandler(mnuModMenu_click));
        foreach (MenuItem mi in mnuMod.MenuItems)
        {
        if (mi.Text == strKeyName)
        {
        //add submenus - mi.ChildItems.Add(/*parameters*/)
        }
        }
        }

        Obviously this assumes that the Text value of each menu item is unique. However, if you have duplicates in the menu it will confuse the user... so take it for what its worth. -- modified at 13:42 Friday 10th March, 2006

        V Offline
        V Offline
        vatzcar
        wrote on last edited by
        #3

        ya, that's the main problem, it's will not only confuse the user but also make coding harder. and most important thing is it's not possible to go with the assumption that every text will be unique. like if i build a library system(for an instance) i may have menu like --> Book > Add ; Member > Add now on click of both add i get that "Add", and my program can't understand which "Add' has been called? Can you give any solution? Beside this i was trying to pass the name through variable, i've tried that in sevaral way but none worked out. if i want to pass the name through a string/array how can i do that? like : MenuItem strMenu = new MenuItem(); //considering strMenu has some value as the name of the menu though this code is wrong, i'v just written it to explain my query. And Thanks for your reply :-D

        1 Reply Last reply
        0
        • V vatzcar

          i want to add Menu>Submenu dynamically, depending upon the data i receive. till now i did : // init. menu MainMenu mnuMain = new MainMenu(); // define menu MenuItem mnuFile = new MenuItem("&File"); MenuItem mnuMod = new MenuItem("&Module"); // add menu to main menu mnuMain.MenuItems.Add(mnuFile); mnuMain.MenuItems.Add(mnuMod); // define submenu MenuItem mnuFExit = new MenuItem("E&xit", new EventHandler(mnuFExit_Click),Shortcut.CtrlX); // add submenu to main menu mnuFile.MenuItems.Add(mnuFExit); foreach (string strKeyName in arrValue) // {arrValue contains some value} { mnuMod.MenuItems.Add(strKeyName,new EventHandler(mnuModMenu_Click)); } till here it's working perfectly. now i want to add submenu to those menu i'v added dynamically. only i have the text of those menu(strKeyName), but don't have any name so can't add menuitems referring them. how can i do that?

          L Offline
          L Offline
          Luis Alonso Ramos
          wrote on last edited by
          #4

          You could save the MenuItems in an array:

          MenuItem[] menuItems = new MenuItem[arrValue.Length];
          
          for(int i = 0; i < arrValue.Length; i++)
          {
              menuItems[i] = mnuMod.MenuItems.Add(arrValue[i], new EventHandler(mnuModMenu_Click));
          }
           
          // Add items to the first submenu
          menuItems[i].MenuItems.Add("First submenu's first item", new EventHandler(...));
          

          Just remember that if you use the same event handler, you'll have to identify in it which menu item was selected, and if you didn't save all the MenuItem references, you'll have to do it by text. -- LuisR


          Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

          The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups