Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / C#
Tip/Trick

Getting the Image Displayed in a Windows Forms TreeNode

Rate me:
Please Sign up or sign in to vote.
4.91/5 (3 votes)
11 Jan 2017CPOL1 min read 20.3K   227   5   2
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)


Written By
Software Developer
Australia Australia
Been programming for 40 years now, starting when I was 13 on DEC PDP 11 (back in the day of paper tape storage, and hex switch boot procedures). Got right into micro-computers from an early age, with machines like the Dick Smith Sorcerer and the CompuColor II. Started CP/M and MS-DOS programming in the mid 1980's. By the end of the '80's, I was just starting to get a good grip on OOP (Had Zortech C++ V1.0).

Got into ATL and COM programming early 2002. As a result, my gutter vocabulary has expanded, but it certainly keeps me off the streets.

Recently, I have had to stop working full time as a programmer due to permanent brain damage as a result of a tumour (I just can't keep up the pace required to meet KPI's). I still like to keep my hand in it, though, and will probably post more articles here as I discover various tricky things.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 1236439012-Jan-17 21:58
Member 1236439012-Jan-17 21:58 
GeneralRe: My vote of 4 Pin
Midi_Mick12-Jan-17 22:17
professionalMidi_Mick12-Jan-17 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.