Skip to content

Commit fe8e039

Browse files
author
Mirroring
committed
Merge commit '2dc2247d6616f895c11328357d64625c678739a8'
2 parents 02682c3 + 2dc2247 commit fe8e039

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItemCollection.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,23 @@ public void AddRange(ToolStripItemCollection toolStripItems)
141141
throw new NotSupportedException(SR.ToolStripItemCollectionIsReadOnly);
142142
}
143143

144+
// Return early if the collection is empty.
145+
if (toolStripItems.Count == 0)
146+
{
147+
return;
148+
}
149+
144150
// ToolStripDropDown will look for PropertyNames.Items to determine if it needs
145151
// to resize itself.
146152
using (new LayoutTransaction(_owner, _owner!, PropertyNames.Items))
147153
{
148-
for (int i = 0; i < toolStripItems.Count; i++)
154+
// Create a temporary array to avoid modifying the original collection during iteration.
155+
// Items will be removed from toolStripsItems collection when they are added to this collection
156+
// if they had a different owner control.
157+
var itemsToAdd = toolStripItems.InnerList.ToArray();
158+
foreach (ToolStripItem item in itemsToAdd)
149159
{
150-
// Items are removed from their origin when added to a different owner.
151-
// Decrement the index to always add the items from index 0 which will preserve
152-
// the original order and avoid a pesky ArgumentOutOfRangeException.
153-
Add(toolStripItems[i--]);
160+
Add(item);
154161
}
155162
}
156163
}

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemCollectionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,36 @@ public void ToolStripItemCollection_AddRange_ToolStripItemCollection_Success()
113113
toolStripDropDownButton.DropDownItems.Add("b");
114114
toolStripDropDownButton.DropDownItems.Add("c");
115115
contextMenuStrip.Items.AddRange(toolStripDropDownButton.DropDownItems);
116+
117+
Assert.Empty(toolStripDropDownButton.DropDownItems);
116118
Assert.Equal(3, contextMenuStrip.Items.Count);
117119

118120
// Validate order.
119121
Assert.Equal("a", contextMenuStrip.Items[0].Text);
120122
Assert.Equal("b", contextMenuStrip.Items[1].Text);
121123
Assert.Equal("c", contextMenuStrip.Items[2].Text);
122124
}
125+
126+
[WinFormsFact]
127+
public void ToolStripItemCollection_AddRange_ToolStripItemCollection_SameOwner_Success()
128+
{
129+
using ToolStrip toolStrip = new();
130+
131+
// Create a ToolStripItemCollection with 2 items
132+
ToolStripItemCollection itemCollection = new(toolStrip,
133+
[
134+
new ToolStripButton("Button 1"),
135+
new ToolStripButton("Button 2")
136+
]);
137+
138+
Assert.Equal(0, toolStrip.Items.Count);
139+
140+
toolStrip.Items.AddRange(itemCollection);
141+
142+
Assert.Equal(2, itemCollection.Count);
143+
Assert.Equal(2, toolStrip.Items.Count);
144+
145+
Assert.Equal("Button 1", toolStrip.Items[0].Text);
146+
Assert.Equal("Button 2", toolStrip.Items[1].Text);
147+
}
123148
}

0 commit comments

Comments
 (0)