Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeView Not Displaying Images from ImageList

I have a TreeView that displays CheckBoxes:

enter image description here

I want to check if a given directory contains an ".mdf" database and if it does, check whether it is attached on the selected server instance. If the database is attached I display an image against that node, and a different image if it is not attached. Note: The images are .png format, size 32x32...

I populate an ImageList from Properties.Resources

mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);

I then loop through the tree and add the relevant image

public static void RecursiveAddImage(TreeNode treeNode, List<string> attachedList)
{
    if (String.Compare(Path.GetExtension(treeNode.Text), ".mdf", true) == 0)
    {
        string databaseName = treeNode.Text.Replace(".mdf", String.Empty);
        if (attachedList.Contains(databaseName))
        {
            treeNode.ImageIndex = 0;
            treeNode.SelectedImageIndex = 0;
        }
        else
        {
            treeNode.ImageIndex = 1;
            treeNode.SelectedImageIndex = 1;
        }
    }
    foreach (TreeNode node in treeNode.Nodes)
        RecursiveAddImage(node, attachedList);
}

The above code goes through the loop with no complaints, finds ".mdf"s and seems to add the relevant ImageIndexes but these do not show up in the TreeView. What am I doing wrong here and can I add the ImageList at design time (something I also can't seem to do)?

I have read several posts and ofcourse the MSDN documantation but I still can't seem to get it working. Any help as always, is much appreciated.

like image 343
MoonKnight Avatar asked Apr 02 '12 15:04

MoonKnight


People also ask

How do I know if TreeView node is selected?

SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.

How do you edit TreeView?

TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView class to true. The default value of the property is false. You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node itself.


2 Answers

TreeNode.StateImageIndex= 0; would set the imagelist images. Make sure the imagelist is binded to Treeview control as mentioned above.

like image 44
Roopz Avatar answered Sep 19 '22 08:09

Roopz


Make sure the TreeView control has the ImageList property set to the correct ImageList reference:

mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);

treeView1.ImageList = mainImageList;
like image 90
LarsTech Avatar answered Sep 20 '22 08:09

LarsTech