Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET TreeView won't show images

Tags:

c#

.net

winforms

Having a problem getting a TreeView control to display node images. The code below works sometimes but fails to show any image at other times.


private TreeNode AddNodeForCore(TreeNode root, Core c) {
    string key = GetImageKey(c);
    TreeNode t = root.Nodes.Add(c.Name, c.Name, key, key);
    t.Tag = c;
    return t;
}

Note that when it fails, the TreeView fails to show any images for any node. The TreeView does have an ImageList assigned to it, and the image key is definitely in the images collection.

Edit:
My google-fu is weak. Can't believe I didn't find that answer myself.

like image 459
Nigel Hawkins Avatar asked Sep 24 '08 07:09

Nigel Hawkins


1 Answers

The helpful bit of the googled posts above is in fact:

"This is a known bug in the Windows XP visual styles implementation. Certain controls, like ImageList, do not get properly initialized when they've been created before the app calls Application.EnableVisualStyles(). The normal Main() implementation in a C#'s Program.cs avoids this. Thanks for posting back!"

So basically, guarantee that Application.EnableVisualStyles() is called before you initialise your imagelist.

like image 140
Matt Jacobsen Avatar answered Oct 02 '22 13:10

Matt Jacobsen