Skip to content

ImageListDesigner.OriginalImageCollection.Remove() and IndexOf() methods do not handle ImageListImage correctly #12291

@ricardobossan

Description

@ricardobossan

.NET version

9.0.100-rc.1.24452.12

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

Description

The Remove(Image value) and IndexOf(Image value) methods in the ImageListDesigner.OriginalImageCollection class currently operate incorrectly when interacting with ImageListImage. These methods are intended to work with ImageListImage objects but only handle the base Image class, causing unintended behavior when removing or searching for items in the collection.

Expected Behavior

  • Remove() should remove an ImageListImage from the collection if it exists.
  • IndexOf() should return the correct index of an ImageListImage in the collection.

Actual Behavior

  • Remove() does not correctly remove the ImageListImage object from the collection, because it only checks for the base Image type.
  • IndexOf() returns -1 or an incorrect index, because it also checks for the base Image type, not ImageListImage.

Affected Code

public int IndexOf(Image? value) => value is Image image ? _items.IndexOf(image) : -1;

public void Remove(Image value)
{
	AssertInvariant();
	_items.Remove(value);
	RecreateHandle();
}

Both methods are currently checking for Image rather than ImageListImage, which leads to issues when working with the collection.

Captura de tela 2024-10-08 191425

Suggested Fix

Modify the Remove() and IndexOf() methods to handle ImageListImage objects explicitly.

  • For the IndexOf() method:
public int IndexOf(ImageListImage? value) => value is ImageListImage imageListImage ? _items.IndexOf(imageListImage) : -1;

int IList.IndexOf(object? value) => value is ImageListImage image ? IndexOf(image) : -1;
  • For the Remove() method:
public void Remove(ImageListImage value)
{
	AssertInvariant();
	_items.Remove(value);
	RecreateHandle();
}

void IList.Remove(object? value)
{
	if (value is ImageListImage image)
	{
			Remove(image);
	}
}

Steps to reproduce

  1. Create an instance of ImageListDesigner.OriginalImageCollection.
  2. Add ImageListImage objects to the collection using the Add method.
  3. Attempt to remove or find an item using the Remove() or IndexOf() method, passing an ImageListImage.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions