File tree Expand file tree Collapse file tree 2 files changed +37
-5
lines changed
src/System/Windows/Forms/Controls/ToolStrips
tests/UnitTests/System/Windows/Forms Expand file tree Collapse file tree 2 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -141,16 +141,23 @@ public void AddRange(ToolStripItemCollection toolStripItems)
141
141
throw new NotSupportedException ( SR . ToolStripItemCollectionIsReadOnly ) ;
142
142
}
143
143
144
+ // Return early if the collection is empty.
145
+ if ( toolStripItems . Count == 0 )
146
+ {
147
+ return ;
148
+ }
149
+
144
150
// ToolStripDropDown will look for PropertyNames.Items to determine if it needs
145
151
// to resize itself.
146
152
using ( new LayoutTransaction ( _owner , _owner ! , PropertyNames . Items ) )
147
153
{
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 )
149
159
{
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 ) ;
154
161
}
155
162
}
156
163
}
Original file line number Diff line number Diff line change @@ -113,11 +113,36 @@ public void ToolStripItemCollection_AddRange_ToolStripItemCollection_Success()
113
113
toolStripDropDownButton . DropDownItems . Add ( "b" ) ;
114
114
toolStripDropDownButton . DropDownItems . Add ( "c" ) ;
115
115
contextMenuStrip . Items . AddRange ( toolStripDropDownButton . DropDownItems ) ;
116
+
117
+ Assert . Empty ( toolStripDropDownButton . DropDownItems ) ;
116
118
Assert . Equal ( 3 , contextMenuStrip . Items . Count ) ;
117
119
118
120
// Validate order.
119
121
Assert . Equal ( "a" , contextMenuStrip . Items [ 0 ] . Text ) ;
120
122
Assert . Equal ( "b" , contextMenuStrip . Items [ 1 ] . Text ) ;
121
123
Assert . Equal ( "c" , contextMenuStrip . Items [ 2 ] . Text ) ;
122
124
}
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
+ toolStrip . Items . Count . Should ( ) . Be ( 0 ) ;
139
+
140
+ toolStrip . Items . AddRange ( itemCollection ) ;
141
+
142
+ itemCollection . Count . Should ( ) . Be ( 2 ) ;
143
+ toolStrip . Items . Count . Should ( ) . Be ( 2 ) ;
144
+
145
+ toolStrip . Items [ 0 ] . Text . Should ( ) . Be ( "Button 1" ) ;
146
+ toolStrip . Items [ 1 ] . Text . Should ( ) . Be ( "Button 2" ) ;
147
+ }
123
148
}
You can’t perform that action at this time.
0 commit comments