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. Copying DropDownItems from one ToolStripDropDownItem to another

Copying DropDownItems from one ToolStripDropDownItem to another

Scheduled Pinned Locked Moved C#
designhelpquestion
5 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.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    Hi all, I have a ToolStripMenuItem in my MenuStrip. I've added a few ToolStripMenuItems as dropdown items at design time. Now I'd like to copy that list of dropdown items to a ToolStripDropDownButton at runtime but I seem to move the items from the ToolStripMenuItem to the ToolStripDropDownButton, not duplicate it. Here is the code I'm using:

    ToolStripItem[] newitems = new ToolStripItem[myToolStripMenuItem.DropDownItems.Count];
    myToolStripMenuItem.DropDownItems.CopyTo(newitems, 0);
    myToolStripDropDownButton.DropDownItems.AddRange(newitems);

    But the problem is that as soon as the last line executes the DropDownItems collection for the ToolStripMenuItem becomes empty. Does anyone have advice for me? Thanks in advance.

    M 1 Reply Last reply
    0
    • D Dewald

      Hi all, I have a ToolStripMenuItem in my MenuStrip. I've added a few ToolStripMenuItems as dropdown items at design time. Now I'd like to copy that list of dropdown items to a ToolStripDropDownButton at runtime but I seem to move the items from the ToolStripMenuItem to the ToolStripDropDownButton, not duplicate it. Here is the code I'm using:

      ToolStripItem[] newitems = new ToolStripItem[myToolStripMenuItem.DropDownItems.Count];
      myToolStripMenuItem.DropDownItems.CopyTo(newitems, 0);
      myToolStripDropDownButton.DropDownItems.AddRange(newitems);

      But the problem is that as soon as the last line executes the DropDownItems collection for the ToolStripMenuItem becomes empty. Does anyone have advice for me? Thanks in advance.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      I think your issue is that your array is essentially storing references of each button - so your last line is in effect adding the exact same buttons (and will automatically become a "move") What you should do is... - Iterate the existing list of items - create new items based on the existing ones - add the new one to where you want them this should provide a "copy" for you

      foreach(ToolStripItem item in myToolStripMenuItem.DropDownItem){
      ToolStripItem newItem = new ToolStripItem();
      //set anything you want the same, maybe there is a function for duplication I don't know
      myToolStripDropDownButton.DropDownItems.Add(newItem);
      }

      return 5;

      D 1 Reply Last reply
      0
      • M musefan

        I think your issue is that your array is essentially storing references of each button - so your last line is in effect adding the exact same buttons (and will automatically become a "move") What you should do is... - Iterate the existing list of items - create new items based on the existing ones - add the new one to where you want them this should provide a "copy" for you

        foreach(ToolStripItem item in myToolStripMenuItem.DropDownItem){
        ToolStripItem newItem = new ToolStripItem();
        //set anything you want the same, maybe there is a function for duplication I don't know
        myToolStripDropDownButton.DropDownItems.Add(newItem);
        }

        return 5;

        D Offline
        D Offline
        Dewald
        wrote on last edited by
        #3

        First of all, the ToolStripItem() constructor is protected so your first line in the foreach loop is already going to cause a problem. Secondly, it's that comment line of yours that is the big headache. It means I have to copy the event handler, the tooltip text and everything. Is it a fact that a ToolStripItem can belong to only one DropDownItem collection?

        M 2 Replies Last reply
        0
        • D Dewald

          First of all, the ToolStripItem() constructor is protected so your first line in the foreach loop is already going to cause a problem. Secondly, it's that comment line of yours that is the big headache. It means I have to copy the event handler, the tooltip text and everything. Is it a fact that a ToolStripItem can belong to only one DropDownItem collection?

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          The code was more for a idea than actual use - I have not worked with them for a while and forget the actual usage but I have created them dynamically is the past so I know you can create an instance somehow. Again, its been a while so I didn't know if there was a 'clone' type of function perhaps. But considering it is a button surely you will only need to copy a few key values such as Text, Icon (if any) and the Click eventHandler. ToolStripItem is a Control, and a Control can only have one parent Control - Basically what you have inadvertently ended up doing is changing the parent control - this is why it appears to "Move" (I don't know much about how this happens 'behind the scenes' thou)

          return 5;

          1 Reply Last reply
          0
          • D Dewald

            First of all, the ToolStripItem() constructor is protected so your first line in the foreach loop is already going to cause a problem. Secondly, it's that comment line of yours that is the big headache. It means I have to copy the event handler, the tooltip text and everything. Is it a fact that a ToolStripItem can belong to only one DropDownItem collection?

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            Had a little play now.. are your items all the same type? i.e. ToolStripButton as you can create all those controls. This is pretty messy but just an idea of what I was playing with...

            foreach (ToolStripItem item in toolStripDropDownButton1.DropDownItems)
            {
            ToolStripItem newItem = new ToolStripMenuItem();
            if (item is ToolStripComboBox)
            newItem = new ToolStripComboBox();
            else if (item is ToolStripTextBox)
            newItem = new ToolStripTextBox();
            newItem.Text = item.Text;
            toolStripDropDownButton2.DropDownItems.Add(newItem);
            }

            return 5;

            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