Here's a little trick for users of TreeViews in .NET.

Highlighting Tree Nodes with an Overlay ImageI wanted a way in Comicster to highlight certain nodes in my TreeView. In my case it was items in your collection that had been matched to an on-line version (stored in a database on a server somewhere).

At first I started playing around with the font-colour of the nodes, but I soon realised that there was a really neat visual way I could do this: A different icon for the node.

Only problem is, I'm trying my best to keep the file size of Comicster down, since it's ClickOnce-deployed and I want the download experience to be fast and smooth. Adding a whole slew of icons for different tree node states was just going to add to the exe size, and give me more icons to have to worry about if I ever update them.

So I struck upon an idea to make a small "overlay" icon, and generate the new "flagged" icons from the original in code.

I already have an ImageList on my main form called "treeImages", and it holds images with names like "folder", "title", "character" etc. I wanted my new, flagged icons to have a predictable name, like "folderLinked", "titleLinked" etc. I added an image to the resources of my application and named it "Linked16" (my naming convention includes the image size in the name).

Enough of this banter. Here's the code:

private void InitializeLinkedTreeImages() 
{ 
    foreach (string key in treeImages.Images.Keys) 
    { 
        Bitmap bmp = new Bitmap(treeImages.Images[key]); 
        Graphics g = Graphics.FromImage(bmp); 
        g.DrawImageUnscaled(Properties.Resources.Linked16, 0, 0); 
        treeImages.Images.Add(key + "Linked", bmp); 
    } 
} 

So now I can set my "linked" tree nodes' icon to "xxxLinked" (where "xxx" is the name of the "normal" icon). If you have dozens of different icons for tree nodes in your application, this little trick will save you from having to maintain two or more versions of each.