From 1a301b436c217b5ab580d2faf8b0cc540a39de39 Mon Sep 17 00:00:00 2001 From: Robert Jordan Date: Wed, 11 Oct 2023 18:06:01 -0400 Subject: [PATCH] Implement ImageListViewSelectedItemCollection IList accessors This implements previously-NotSupported IList accessors in order to fix issues where methods accepting an IEnumerable will try to optimize if a more useful interface like IList exists. For example, Linq's FirstOrDefault will fail because it can't use the IList.this[int index] accessor. And passing SelectedItems to a List constructor will fail because IList.CopyTo is not supported. The obsolete notice on IList.IndexOf has been removed, since ImageListViewItem.Index does not return the same value as the index in the selected items collection. --- .../ImageListViewSelectedItemCollection.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ImageListView/ImageListViewSelectedItemCollection.cs b/ImageListView/ImageListViewSelectedItemCollection.cs index 2589208f..de14b308 100644 --- a/ImageListView/ImageListViewSelectedItemCollection.cs +++ b/ImageListView/ImageListViewSelectedItemCollection.cs @@ -155,7 +155,12 @@ void ICollection.Clear() /// The zero-based index in at which copying begins. void ICollection.CopyTo(ImageListViewItem[] array, int arrayIndex) { - throw new NotSupportedException(); + int i = 0; + foreach (ImageListViewItem item in this) + { + array[arrayIndex + i] = item; + i++; + } } /// /// Determines the index of a specific item in the . @@ -164,10 +169,16 @@ void ICollection.CopyTo(ImageListViewItem[] array, int arrayI /// /// The index of if found in the list; otherwise, -1. /// - [Obsolete("Use ImageListViewItem.Index property instead.")] int IList.IndexOf(ImageListViewItem item) { - throw new NotSupportedException(); + int i = 0; + foreach (ImageListViewItem selectedItem in this) + { + if (selectedItem == item) + return i; + i++; + } + return -1; } /// /// Inserts an item to the at the specified index. @@ -204,7 +215,7 @@ ImageListViewItem IList.this[int index] { get { - throw new NotSupportedException(); + return this[index]; } set { @@ -323,4 +334,4 @@ public void Reset() #endregion } } -} \ No newline at end of file +}