Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Getting the Image Displayed in a Windows Forms TreeNode

4.91/5 (3 votes)
11 Jan 2017CPOL1 min read 20.8K   231  
Extension methods to get the index or key of the image currently displayed in the node of a Windows.Forms.TreeView control

Introduction

As described in the Microsoft documentation:

Quote:

The ImageKey and ImageIndex properties are mutually exclusive; therefore, if one property is set, the other is ignored. If you set the ImageKey property, the ImageIndex property is automatically set to -1. Alternatively, if you set ImageIndex, ImageKey is automatically set to an empty string ("").

This can be a bit of a pain when the images are set dynamically based on conditions in your code, sometimes by key, and other times by index. These four simple extension methods allow you to return either the key or the index of the image currently displayed, regardless of which property was set.

Background

I am currently building a form with a TreeView, where the "normal" images are assigned by a key returned from the object that the node describes. However, if the node is selected as special in some way (the default item, or a prominent item), then the ImageIndex is assigned the appropriate value. I use Linq to "find" particular nodes based on the image that has been assigned to it, and reassign the special images on button clicks from the user.

Using the Code

Just copy the following code into one of your modules:

C#
public static class TreeNodeImageExtension {
    public static int ImageNumber(this TreeNode node) {
        if (node.ImageIndex == -1 && !string.IsNullOrEmpty(node.ImageKey))
            return node.TreeView.ImageList.Images.IndexOfKey(node.ImageKey);
        return node.ImageIndex;
    }
    public static int SelectedImageNumber(this TreeNode node) {
        if (node.SelectedImageIndex == -1 && !string.IsNullOrEmpty(node.SelectedImageKey))
            return node.TreeView.ImageList.Images.IndexOfKey(node.SelectedImageKey);
        return node.SelectedImageIndex;

    }
    public static string ImageName(this TreeNode node) {
        if (string.IsNullOrEmpty(node.ImageKey) && node.ImageIndex >= 0)
            return node.TreeView.ImageList.Images.Keys[node.ImageIndex];
        return node.ImageKey;
    }
    public static string SelectedImageName(this TreeNode node) {
        if (string.IsNullOrEmpty(node.SelectedImageKey) && node.SelectedImageIndex >= 0)
            return node.TreeView.ImageList.Images.Keys[node.SelectedImageIndex];
        return node.SelectedImageKey;
    }
}

You can then get the ImageIndex or SelectedImageIndex from the node by calling the ImageNumber method on the required node, or the ImageKey or SelectedImageKey by calling the ImageName method.

It works by checking if the current value has been set with the required property. If not, it goes back to the ImageList control assigned to the TreeView, and determines the required value based upon the assigned value of the other property.

History

  • 11th January, 2017: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)