diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewGroupCollection.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewGroupCollection.cs index 350dd4f5db1..fb4832f4e59 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewGroupCollection.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewGroupCollection.cs @@ -45,6 +45,7 @@ public ListViewGroup this[int index] return; } + ThrowArgumentExceptionIfOtherListView(value); CheckListViewItems(value); value.ListView = _listView; List[index] = value; @@ -92,6 +93,7 @@ public ListViewGroup? this[string key] if (index != -1) { + ThrowArgumentExceptionIfOtherListView(value); _list[index] = value; } } @@ -119,6 +121,7 @@ public int Add(ListViewGroup group) return -1; } + ThrowArgumentExceptionIfOtherListView(group); CheckListViewItems(group); group.ListView = _listView; int index = ((IList)List).Add(group); @@ -244,6 +247,7 @@ public void Insert(int index, ListViewGroup group) return; } + ThrowArgumentExceptionIfOtherListView(group); CheckListViewItems(group); group.ListView = _listView; List.Insert(index, group); @@ -308,4 +312,12 @@ private void ThrowInvalidOperationExceptionIfVirtualMode() throw new InvalidOperationException(SR.ListViewCannotAddGroupsToVirtualListView); } } + + private void ThrowArgumentExceptionIfOtherListView(ListViewGroup group) + { + if (group.ListView is not null && group.ListView != _listView) + { + throw new ArgumentException(string.Format(SR.OnlyOneControl, group.Header), nameof(group)); + } + } } diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/ListViewGroupCollectionTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/ListViewGroupCollectionTests.cs index c70fc76a884..ec2b69a048c 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/ListViewGroupCollectionTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/ListViewGroupCollectionTests.cs @@ -130,11 +130,13 @@ public void ListViewGroupCollection_Item_SetAlreadyInCollection_Nop(int index) } [WinFormsFact] - public void ListViewGroupCollection_Item_SetAlreadyInOtherCollection_GetReturnsExpected() + public void ListViewGroupCollection_Item_SetAlreadyInOtherCollection_ThrowsArgumentException() { using ListView listView = new(); ListViewGroupCollection collection = listView.Groups; - collection.Add(new ListViewGroup()); + + ListViewGroup existingGroup = new(); + collection.Add(existingGroup); using ListView otherListView = new(); ListViewGroupCollection otherCollection = otherListView.Groups; @@ -142,11 +144,15 @@ public void ListViewGroupCollection_Item_SetAlreadyInOtherCollection_GetReturnsE ListViewGroup group = new(); otherCollection.Add(group); - collection[0] = group; - Assert.Same(group, collection[0]); - Assert.Same(listView, group.ListView); - Assert.Equal(group, Assert.Single(collection)); - Assert.Equal(group, Assert.Single(otherCollection)); + Assert.Throws( + "group", + () => collection[0] = group); + + Assert.Same(existingGroup, Assert.Single(collection)); + Assert.Same(listView, existingGroup.ListView); + + Assert.Same(otherListView, group.ListView); + Assert.Same(group, Assert.Single(otherCollection)); } [WinFormsFact] @@ -466,11 +472,12 @@ public void ListViewGroupCollection_Add_AlreadyInOtherCollection_GetReturnsExpec ListViewGroup group = new(); otherCollection.Add(group); - // The group appears to belong to two list views. - collection.Add(group); - Assert.Same(group, collection[0]); - Assert.Same(listView, group.ListView); - Assert.Equal(group, Assert.Single(collection)); + Assert.Throws( + "group", + () => collection.Add(group)); + + Assert.Empty(collection); + Assert.Same(otherListView, group.ListView); Assert.Equal(group, Assert.Single(otherCollection)); } @@ -812,11 +819,13 @@ public void ListViewGroupCollection_Insert_AlreadyInOtherCollection_GetReturnsEx ListViewGroup group = new(); otherCollection.Add(group); - // The group appears to belong to two list views. - collection.Insert(0, group); - Assert.Same(group, collection[0]); - Assert.Same(listView, group.ListView); - Assert.Equal(group, collection[0]); + Assert.Throws( + "group", + () => collection.Insert(0, group)); + + Assert.Single(collection); + Assert.NotSame(group, collection[0]); + Assert.Same(otherListView, group.ListView); Assert.Equal(group, Assert.Single(otherCollection)); } @@ -1133,4 +1142,70 @@ public void ListViewGroupCollection_Insert_DoesNotWork_IfVirtualMode(bool create Assert.Throws(() => listView.Groups.Insert(0, new ListViewGroup())); Assert.Equal(createControl, listView.IsHandleCreated); } + + [WinFormsFact] + public void ListViewGroupCollection_IListAdd_GroupAlreadyInOtherCollection_ThrowsArgumentException() + { + using ListView listView = new(); + IList collection = listView.Groups; + + using ListView otherListView = new(); + ListViewGroupCollection otherCollection = otherListView.Groups; + + ListViewGroup group = new("Group"); + otherCollection.Add(group); + + Assert.Throws("group", () => collection.Add(group)); + Assert.Empty(collection); + Assert.Same(otherListView, group.ListView); + Assert.Same(group, Assert.Single(otherCollection)); + } + + [WinFormsFact] + public void ListViewGroupCollection_AddRange_GroupAlreadyInOtherCollection_ThrowsArgumentException() + { + using ListView listView = new(); + ListViewGroupCollection collection = listView.Groups; + + using ListView otherListView = new(); + ListViewGroupCollection otherCollection = otherListView.Groups; + + ListViewGroup validGroup = new(); + ListViewGroup group = new(); + otherCollection.Add(group); + + Assert.Throws( + "group", + () => collection.AddRange(validGroup, group)); + + Assert.Same(validGroup, Assert.Single(collection)); + Assert.Same(listView, validGroup.ListView); + + Assert.Same(otherListView, group.ListView); + Assert.Same(group, Assert.Single(otherCollection)); + } + + [WinFormsFact] + public void ListViewGroupCollection_Item_SetByKeyAlreadyInOtherCollection_ThrowsArgumentException() + { + using ListView listView = new(); + ListViewGroupCollection collection = listView.Groups; + + ListViewGroup existingGroup = new() { Name = "key" }; + collection.Add(existingGroup); + + using ListView otherListView = new(); + ListViewGroupCollection otherCollection = otherListView.Groups; + + ListViewGroup group = new(); + otherCollection.Add(group); + + Assert.Throws(() => collection["key"] = group); + + Assert.Same(existingGroup, Assert.Single(collection)); + Assert.Same(listView, existingGroup.ListView); + + Assert.Same(otherListView, group.ListView); + Assert.Same(group, Assert.Single(otherCollection)); + } }