Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ListViewGroup this[int index]
return;
}

ThrowArgumentExceptionIfOtherListView(value);
CheckListViewItems(value);
value.ListView = _listView;
List[index] = value;
Expand Down Expand Up @@ -92,6 +93,7 @@ public ListViewGroup? this[string key]

if (index != -1)
{
ThrowArgumentExceptionIfOtherListView(value);
_list[index] = value;
}
}
Expand Down Expand Up @@ -119,6 +121,7 @@ public int Add(ListViewGroup group)
return -1;
}

ThrowArgumentExceptionIfOtherListView(group);
CheckListViewItems(group);
group.ListView = _listView;
int index = ((IList)List).Add(group);
Expand Down Expand Up @@ -244,6 +247,7 @@ public void Insert(int index, ListViewGroup group)
return;
}

ThrowArgumentExceptionIfOtherListView(group);
CheckListViewItems(group);
group.ListView = _listView;
List.Insert(index, group);
Expand Down Expand Up @@ -308,4 +312,12 @@ private void ThrowInvalidOperationExceptionIfVirtualMode()
throw new InvalidOperationException(SR.ListViewCannotAddGroupsToVirtualListView);
}
}

private void ThrowArgumentExceptionIfOtherListView(ListViewGroup group)
Comment thread
abineshPalanisamy marked this conversation as resolved.
{
if (group.ListView is not null && group.ListView != _listView)
{
throw new ArgumentException(string.Format(SR.OnlyOneControl, group.Header), nameof(group));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,29 @@ 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;

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<ArgumentException>(
"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]
Expand Down Expand Up @@ -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<ArgumentException>(
"group",
() => collection.Add(group));

Assert.Empty(collection);
Assert.Same(otherListView, group.ListView);
Assert.Equal(group, Assert.Single(otherCollection));
}

Expand Down Expand Up @@ -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<ArgumentException>(
"group",
() => collection.Insert(0, group));

Assert.Single(collection);
Assert.NotSame(group, collection[0]);
Assert.Same(otherListView, group.ListView);
Assert.Equal(group, Assert.Single(otherCollection));
}

Expand Down Expand Up @@ -1133,4 +1142,70 @@ public void ListViewGroupCollection_Insert_DoesNotWork_IfVirtualMode(bool create
Assert.Throws<InvalidOperationException>(() => 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<ArgumentException>("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<ArgumentException>(
"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<ArgumentException>(() => 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));
}
}