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. Iterarting menu item

Iterarting menu item

Scheduled Pinned Locked Moved C#
data-structures
3 Posts 2 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.
  • A Offline
    A Offline
    Amar Chaudhary
    wrote on last edited by
    #1

    when I iterate through form controls and their child controls it works fine for every thing else expect when it comes to menu items it only returns only the top item and not the child ones the code I am using is

      void ModifyControls(Control.ControlCollection ControlCollection)
            {
                //iterator
                for (int i = 0; i < ControlCollection.Count; i++)
                {
                    if (ControlCollection[i].GetType() != typeof(Menu))
                    {
                        // add control
                        Array.Resize(ref Controls, Controls.Length + 1);
                        Controls[Controls.Length - 1] = new MyControls(ControlCollection[i]);
    
                        // save control specific formating and apply it
                        Controls[Controls.Length - 1].SaveFormating();
                        //itrate through child controls
                        ModifyControls(ControlCollection[i].Controls);
                    }
                }
            }
    

    Thanks and Regards Amar Chaudhary

    It is Good to be Important but! it is more Important to be Good

    E 1 Reply Last reply
    0
    • A Amar Chaudhary

      when I iterate through form controls and their child controls it works fine for every thing else expect when it comes to menu items it only returns only the top item and not the child ones the code I am using is

        void ModifyControls(Control.ControlCollection ControlCollection)
              {
                  //iterator
                  for (int i = 0; i < ControlCollection.Count; i++)
                  {
                      if (ControlCollection[i].GetType() != typeof(Menu))
                      {
                          // add control
                          Array.Resize(ref Controls, Controls.Length + 1);
                          Controls[Controls.Length - 1] = new MyControls(ControlCollection[i]);
      
                          // save control specific formating and apply it
                          Controls[Controls.Length - 1].SaveFormating();
                          //itrate through child controls
                          ModifyControls(ControlCollection[i].Controls);
                      }
                  }
              }
      

      Thanks and Regards Amar Chaudhary

      It is Good to be Important but! it is more Important to be Good

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

      Greetings Amar, If you look at how the MenuStrip control works you will notice that it houses a different type of collection, the ToolStripItemCollection in its MenuStrip.Items property. You need to iterate each ToolStripMenuItem in that collection to get its children. After that, the ToolStripMenuItem has another ToolStripItemCollection in it's DropDownItems property, you now need to iterate through this collection to get it's children and so forth... consider the following example: void ListControls(Control parentControl) { foreach (Control control in parentControl.Controls) { MessageBox.Show("Control Type : " + control.GetType().ToString() + "\n" + "Has Children : " + (control.Controls.Count > 0 ? "Yes" : "No") + "\n"); // Perform the child-find functionality on the ControlCollection here. if (control.Controls.Count > 0) { // Call ourselves to discover additional children... this.ListControls(control); } // Get the sub menus of a MenuStrip. // * Test for the type of control, we're looking for the MenuStrip here... // TODO: Add this to a different function to nest-find child tool strips. if (control.GetType() == typeof(MenuStrip)) { // As you can see, the menu houses a collection of items not controls, notice this is now a ToolStripItemcollection not ControlCollection. foreach (ToolStripMenuItem toolStripMenuItem in ((MenuStrip)control).Items) { MessageBox.Show("---> Menu Strip '" + ((MenuStrip)control).Text + "' has '" + toolStripMenuItem.Text + "' as a child item."); // Test if tool strip has child items, notice this is a ToolStripItemcollection too. if (toolStripMenuItem.DropDownItems.Count > 0) { // Call your nested-find function... } } } } } Basically, the MenuStrip needs to be treated like a TreeView as it can contain a complex hierarchy. I hope this helps... Have a great weekend!

      Fernando Mendes Senior .NET Developer, Architect

      A 1 Reply Last reply
      0
      • E ElSpinos

        Greetings Amar, If you look at how the MenuStrip control works you will notice that it houses a different type of collection, the ToolStripItemCollection in its MenuStrip.Items property. You need to iterate each ToolStripMenuItem in that collection to get its children. After that, the ToolStripMenuItem has another ToolStripItemCollection in it's DropDownItems property, you now need to iterate through this collection to get it's children and so forth... consider the following example: void ListControls(Control parentControl) { foreach (Control control in parentControl.Controls) { MessageBox.Show("Control Type : " + control.GetType().ToString() + "\n" + "Has Children : " + (control.Controls.Count > 0 ? "Yes" : "No") + "\n"); // Perform the child-find functionality on the ControlCollection here. if (control.Controls.Count > 0) { // Call ourselves to discover additional children... this.ListControls(control); } // Get the sub menus of a MenuStrip. // * Test for the type of control, we're looking for the MenuStrip here... // TODO: Add this to a different function to nest-find child tool strips. if (control.GetType() == typeof(MenuStrip)) { // As you can see, the menu houses a collection of items not controls, notice this is now a ToolStripItemcollection not ControlCollection. foreach (ToolStripMenuItem toolStripMenuItem in ((MenuStrip)control).Items) { MessageBox.Show("---> Menu Strip '" + ((MenuStrip)control).Text + "' has '" + toolStripMenuItem.Text + "' as a child item."); // Test if tool strip has child items, notice this is a ToolStripItemcollection too. if (toolStripMenuItem.DropDownItems.Count > 0) { // Call your nested-find function... } } } } } Basically, the MenuStrip needs to be treated like a TreeView as it can contain a complex hierarchy. I hope this helps... Have a great weekend!

        Fernando Mendes Senior .NET Developer, Architect

        A Offline
        A Offline
        Amar Chaudhary
        wrote on last edited by
        #3

        thanks for the help Amar Chaudhary

        It is Good to be Important but! it is more Important to be Good

        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