From c669b50eeabb42a59266d10801338b5f9774d537 Mon Sep 17 00:00:00 2001
From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com>
Date: Wed, 10 Jun 2026 10:13:13 +0530
Subject: [PATCH 1/4] Added fix for the Fix_Issue_4001
Added fix for the Fix_Issue_4001
---
src/System.Windows.Forms/Resources/SR.resx | 3 +++
.../Forms/Controls/ListView/ListViewGroupCollection.cs | 10 ++++++++++
2 files changed, 13 insertions(+)
diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx
index 53d5073b748..dfab7834c5d 100644
--- a/src/System.Windows.Forms/Resources/SR.resx
+++ b/src/System.Windows.Forms/Resources/SR.resx
@@ -7045,4 +7045,7 @@ Stack trace where the illegal operation occurred was:
The FormScreenCaptureMode property can only be changed on top-level Forms with their TopLevel property set to true.
+
+ Cannot add or insert a ListViewGroup that already belongs to another ListView.
+
\ No newline at end of file
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..77d8777070e 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
@@ -119,6 +119,7 @@ public int Add(ListViewGroup group)
return -1;
}
+ ThrowArgumentExceptionIfOtherListView(group);
CheckListViewItems(group);
group.ListView = _listView;
int index = ((IList)List).Add(group);
@@ -244,6 +245,7 @@ public void Insert(int index, ListViewGroup group)
return;
}
+ ThrowArgumentExceptionIfOtherListView(group);
CheckListViewItems(group);
group.ListView = _listView;
List.Insert(index, group);
@@ -308,4 +310,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(SR.ListViewGroupBelongsToAnotherListView, nameof(group));
+ }
+ }
}
From b143232a6c09616684dc0b03cc55e332b7b9fa62 Mon Sep 17 00:00:00 2001
From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com>
Date: Mon, 6 Jul 2026 16:51:21 +0530
Subject: [PATCH 2/4] Addressed the review corrections and add unit test cases
for Fix_Issue_4001
Addressed the review corrections and add unit test cases for Fix_Issue_4001
---
src/System.Windows.Forms/Resources/SR.resx | 3 -
.../ListView/ListViewGroupCollection.cs | 2 +-
.../Forms/ListViewGroupCollectionTests.cs | 72 +++++++++++++++++++
3 files changed, 73 insertions(+), 4 deletions(-)
diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx
index dfab7834c5d..53d5073b748 100644
--- a/src/System.Windows.Forms/Resources/SR.resx
+++ b/src/System.Windows.Forms/Resources/SR.resx
@@ -7045,7 +7045,4 @@ Stack trace where the illegal operation occurred was:
The FormScreenCaptureMode property can only be changed on top-level Forms with their TopLevel property set to true.
-
- Cannot add or insert a ListViewGroup that already belongs to another ListView.
-
\ No newline at end of file
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 77d8777070e..1492dd85a19 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
@@ -315,7 +315,7 @@ private void ThrowArgumentExceptionIfOtherListView(ListViewGroup group)
{
if (group.ListView is not null && group.ListView != _listView)
{
- throw new ArgumentException(SR.ListViewGroupBelongsToAnotherListView, nameof(group));
+ 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..e9f0ee6f010 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
@@ -1133,4 +1133,76 @@ 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_Add_GroupAlreadyInOtherCollection_ThrowsArgumentException()
+ {
+ using ListView listView = new();
+ ListViewGroupCollection 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_Insert_GroupAlreadyInOtherCollection_ThrowsArgumentException()
+ {
+ using ListView listView = new();
+ ListViewGroupCollection collection = listView.Groups;
+
+ using ListView otherListView = new();
+ ListViewGroupCollection otherCollection = otherListView.Groups;
+
+ ListViewGroup group = new("Group");
+ otherCollection.Add(group);
+
+ Assert.Throws("group", () => collection.Insert(0, group));
+ Assert.Empty(collection);
+ Assert.Same(otherListView, group.ListView);
+ Assert.Same(group, Assert.Single(otherCollection));
+ }
+
+ [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 group = new("Group");
+ otherCollection.Add(group);
+
+ Assert.Throws("group", () => collection.AddRange(group));
+ Assert.Empty(collection);
+ Assert.Same(otherListView, group.ListView);
+ Assert.Same(group, Assert.Single(otherCollection));
+ }
}
From a8844cb426a633d13d13c92d821f79283370461e Mon Sep 17 00:00:00 2001
From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com>
Date: Thu, 9 Jul 2026 13:06:37 +0530
Subject: [PATCH 3/4] Updated the test cases for Fix_Issue_4001
Updated the test cases for Fix_Issue_4001
---
.../Forms/ListViewGroupCollectionTests.cs | 59 ++++---------------
1 file changed, 13 insertions(+), 46 deletions(-)
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 e9f0ee6f010..bc57f8e9b41 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
@@ -466,11 +466,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 +813,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));
}
@@ -1134,42 +1137,6 @@ public void ListViewGroupCollection_Insert_DoesNotWork_IfVirtualMode(bool create
Assert.Equal(createControl, listView.IsHandleCreated);
}
- [WinFormsFact]
- public void ListViewGroupCollection_Add_GroupAlreadyInOtherCollection_ThrowsArgumentException()
- {
- using ListView listView = new();
- ListViewGroupCollection 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_Insert_GroupAlreadyInOtherCollection_ThrowsArgumentException()
- {
- using ListView listView = new();
- ListViewGroupCollection collection = listView.Groups;
-
- using ListView otherListView = new();
- ListViewGroupCollection otherCollection = otherListView.Groups;
-
- ListViewGroup group = new("Group");
- otherCollection.Add(group);
-
- Assert.Throws("group", () => collection.Insert(0, group));
- Assert.Empty(collection);
- Assert.Same(otherListView, group.ListView);
- Assert.Same(group, Assert.Single(otherCollection));
- }
-
[WinFormsFact]
public void ListViewGroupCollection_IListAdd_GroupAlreadyInOtherCollection_ThrowsArgumentException()
{
From 8de4bb9908f2dcdc814a6cee7b19335f0721ebd1 Mon Sep 17 00:00:00 2001
From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com>
Date: Fri, 10 Jul 2026 13:56:15 +0530
Subject: [PATCH 4/4] Updated the review correction for the Fix_Issue_4001
Updated the review correction for the Fix_Issue_4001
---
.../ListView/ListViewGroupCollection.cs | 2 +
.../Forms/ListViewGroupCollectionTests.cs | 56 +++++++++++++++----
2 files changed, 48 insertions(+), 10 deletions(-)
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 1492dd85a19..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;
}
}
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 bc57f8e9b41..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]
@@ -1164,11 +1170,41 @@ public void ListViewGroupCollection_AddRange_GroupAlreadyInOtherCollection_Throw
using ListView otherListView = new();
ListViewGroupCollection otherCollection = otherListView.Groups;
- ListViewGroup group = new("Group");
+ ListViewGroup validGroup = new();
+ ListViewGroup group = new();
otherCollection.Add(group);
- Assert.Throws("group", () => collection.AddRange(group));
- Assert.Empty(collection);
+ 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));
}