From 1a905a4fae21f86254836bef3bb868b0762124aa Mon Sep 17 00:00:00 2001 From: AraHaan Date: Thu, 22 Feb 2024 13:02:33 -0500 Subject: [PATCH] Added version of ListViewItemCollection.AddRange() which accepts IList. Fixes https://github.com/dotnet/winforms/issues/10924. --- .../ListView.ListViewItemCollection.cs | 2 +- .../ListView.ListViewItemCollectionTests.cs | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.ListViewItemCollection.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.ListViewItemCollection.cs index 6af6c9273e5..001504a1b1f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.ListViewItemCollection.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.ListViewItemCollection.cs @@ -248,7 +248,7 @@ public void AddRange(params ListViewItem[] items) InnerList.AddRange(items); } - public void AddRange(ListViewItemCollection items) + public void AddRange(IList items) { ArgumentNullException.ThrowIfNull(items); diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListView.ListViewItemCollectionTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListView.ListViewItemCollectionTests.cs index 0a58cff819a..c8ee48b6e0d 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListView.ListViewItemCollectionTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListView.ListViewItemCollectionTests.cs @@ -98,4 +98,24 @@ public void ListViewItemCollection_Find_NullOrEmptyKey_ThrowsArgumentNullExcepti Assert.Throws("key", () => collection.Find(key, searchAllSubItems: true)); Assert.Throws("key", () => collection.Find(key, searchAllSubItems: false)); } + + [WinFormsFact] + public void ListViewItemCollection_Given_That_We_Want_To_Use_List_ListViewItem_To_Add_Items() + { + using ListView listView = new(); + var list = new List(); + var keys = new List(); + for (var i = 0; i < 10; i++) + { + list.Add(new ListViewItem() { $"item{i}" }); + keys.Add($"item{i}"); + } + + ListView.ListViewItemCollection collection = listView.Items; + collection.AddRange(list); + foreach (var item in list) + { + Asset.Equal(item, collection.Find(key[list.IndexOf(item)], searchAllSubItems: false))l + } + } }