dotnet / dotnet/winforms

TreeNodes without an image set are changed to the default image if ImageList is changed

Open
#13,976 1 comment 0 reactions 1 assignee Claimed by @LeafShi1 View on GitHub
:beetle: bug
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

### .NET version

.NET Framework 4.8, .NET 8, .NET 9

### Did it work in .NET Framework?

No

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

_No response_

### Issue description

When an image is added to or removed from an ImageList which is used for a TreeView, all nodes which don't have their own image set and thus use the default image of the TreeView, use the first image in the ImageList afterwards and not the set default image.

It looks like there is a bug in `TreeNode.UpdateImage` https://github.com/dotnet/winforms/blob/4edb5018023c639ed475bae111b0ceaf93bd508c/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeNode.cs#L2196 which is called from the ImageListChangedHandle handler: https://github.com/dotnet/winforms/blob/4edb5018023c639ed475bae111b0ceaf93bd508c/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.cs#L1710

It doesn't check `IsSpecialImageIndex` and use the TreeView's imageIndexer in this case, like the `UpdateNode` method above.

### Steps to reproduce

1. Create an ImageList with a few images.
2. Create a TreeView and set the ImageList.
3. Set one of the images as default on the TreeView using `treeView.ImageKey`. Do not use the first image in the ImageList.
4. Add some nodes to the tree. Do not set an image on them so that they get the default tree-wide image.
5. Add or remove an image from the image list at runtime.
6. Observe that the image on the nodes, which had the default set, gets set to the first image of the ImageList instead of keeping their image. The TreeView is not directly modified in step 5.

See https://github.com/jmairboeck/TreeViewImageBug for an example project to reproduce the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.