Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to set image to leaf node only, but it appears that the non-leaf nodes are set automatically.

What I have tried:

tv.ImageList = images
List<TreeNode> Nodes = tv.GetAllTreeNodes().ToList();
            foreach (TreeNode tn in Nodes)
            {
                if (tn.GetNodeCount(false) != 0)
                {
                    tn.ImageIndex = -1;
                }
                else
                {
                       tn.ImageKey ="myimage";
                 }
}


the leaf node set correctly but there is an image on the non-leaf node as well.
Posted
Updated 30-Sep-17 2:43am
v2
Comments
Richard MacCutchan 30-Sep-17 4:16am    
tn.ImageIndex ="myimage";
That will not even compile. TreeNode.ImageIndex is an integer not a string.
855 30-Sep-17 8:42am    
typo should be ImageKey
Richard MacCutchan 30-Sep-17 10:58am    
Then I think you need to use your debugger to see what is happening here. You may also like to ask yourself why you are creating that List<t> when the TreeView already contains the collection of all nodes.
855 30-Sep-17 21:53pm    
it's not a real code in project. i just want to show when the image of non-leaf nodes are set tn.ImageIndex = -1; , a default icon still there and how to remove it.
Richard MacCutchan 1-Oct-17 2:56am    
Then do what I suggest and use your debugger to find out what is happening in this code. We cannot guess what values those variables contain.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900