Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a tree with image. parent node has different image from child nodes.
When i click on child node, Image of parent node is changing. I could not figure out why. Any solution will be great help.

Here is the code sample i m using to populate tree.

C#
private void LoadTree()
{
                TreeNode trParent = new TreeNode();
                TreeNode trChild = new TreeNode();
                TreeNodeCollection trNodeCol = Treeview1.Nodes;

                Treeview1.ImageList = ImageList1;
                trParent.Text = "Users";
                trParent.SelectedImageIndex = 1;
                Treeview1.Nodes.Add(trParent);

                if (trNodeCol.Count>0)
                {
                       Treeview1.SelectedNode = trNodeCol[0];
                       trParent = Treeview1.SelectedNode;
                    
                        trChild = new TreeNode();
                        trChild.Text = "User1";
                        trChild.Tag = "1";
                        trChild.ImageIndex = 0;
                        trParent.Nodes.Add(trChild);

                        trChild = new TreeNode();
                        trChild.Text = "User2";
                        trChild.Tag = "2";
                        trChild.ImageIndex = 0;                    
                        trParent.Nodes.Add(trChild);
                }
                Treeview1.ExpandAll();

}
Posted
Updated 1-Feb-11 17:09pm
v2
Comments
TweakBird 1-Feb-11 23:09pm    
Use <pre> tag for code blocks.

Images of the TreeView need some care. You don't show all your code. So, please check up the following: you need two sets of ImageLists: ImageList and StateImageList; one of then or both can be null, all images should be 16x16. Checkup your assignments of for ImageIndex, SelectedImageIndex and StateImageIndex.

You should be careful with "no image" situation and maintain consistency: ImageIndex, SelectedImageIndex control "normal" (non-state) image and StateImageIndex controls state image. If you're using them both, the indexes should always be 0..Count, where Count is the count of images in the respective list. In other words, you either always show both images or always only one of them, never changing this number of image for each nodes.

Also, do you handle BeforeSelect or AfterSelect event (usually you should)? Do you modify any indexes described above in the handlers? You could have a subtle bug there. If you do (and did not find a problem yet), please post this code.

(Important! Please, do not post it as an Answer. for follow-up, please use "Improve Question" or "Add Comment".)

—SA
 
Share this answer
 
v2
Could it be the parent is displaying ImageList1[0] and then when you select a child node it is displaying ImageList[1] because you have set the trParent.SelectedImageIndex = 1.

Maybe you meant to set the trParent.ImageIndex = 1 instead.
 
Share this answer
 

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