Copying DropDownItems from one ToolStripDropDownItem to another
-
Hi all, I have a
ToolStripMenuItem
in myMenuStrip
. I've added a fewToolStripMenuItem
s as dropdown items at design time. Now I'd like to copy that list of dropdown items to aToolStripDropDownButton
at runtime but I seem to move the items from theToolStripMenuItem
to theToolStripDropDownButton
, 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 theToolStripMenuItem
becomes empty. Does anyone have advice for me? Thanks in advance. -
Hi all, I have a
ToolStripMenuItem
in myMenuStrip
. I've added a fewToolStripMenuItem
s as dropdown items at design time. Now I'd like to copy that list of dropdown items to aToolStripDropDownButton
at runtime but I seem to move the items from theToolStripMenuItem
to theToolStripDropDownButton
, 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 theToolStripMenuItem
becomes empty. Does anyone have advice for me? Thanks in advance.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;
-
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;
First of all, the
ToolStripItem()
constructor is protected so your first line in theforeach
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 aToolStripItem
can belong to only oneDropDownItem
collection? -
First of all, the
ToolStripItem()
constructor is protected so your first line in theforeach
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 aToolStripItem
can belong to only oneDropDownItem
collection?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;
-
First of all, the
ToolStripItem()
constructor is protected so your first line in theforeach
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 aToolStripItem
can belong to only oneDropDownItem
collection?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;