tags:

views:

315

answers:

3

I am trying to make a folder explorer using TreeView control. now, I added an ImageList with two images - one for a folder and the other for selected folder. I used the 'BeforeExpand' event to change the icon of the current node (folder). The problem is that I dont know how to change it back when the user selects other folder... what event can I use? Maybe I dont use it right... ?

A: 

I think you have to keep a reference of the last node expanded, so you can change it later

Jhonny D. Cano -Leftware-
A: 

hmm I don't know if I understand correctly but actually, in a folder explorer context, if the user expands another node, you don't need to do any specific action on the node you have previously expanded. If the user clicks again on it, then you can use the BeforeCollapse event to change it back. Would it fit your needs?

PierrOz
A: 

I'll assume here you only want to change the TreeNode image shown when a given Node is selected. And I'm not going to deal here with the issue of showing 'folder icons for every TreeNode with Child Nodes: I think if you grasp what's going on here, you'll have no problem adding a (third) image and "doing the right thing" to make folders explicit: and then you can extend that, if you wish, so Nodes with "no child nodes" can have different icons for selected/unselected.

(1 add an ImageList to your Form with two images: the image with index #0 will be the default (unselected) TreeNode image.

(2 add an AfterSelect Event Handler to your TreeView : change its image to the "selected" image.

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        e.Node.SelectedImageIndex = 1;
    }

You'll note that as the selected Node is changed: the previously selected TreeNode image will automatically revert to use its default image (index #0 in the ImageList).

Note : imho it's important here to distinguish between expanding a TreeNode in a TreeView and selecting a TreeNode in a TreeView: you can expand any TreeNode (with child nodes) by clicking on the "+" icon : but that will not select it. Double-Clicking will, of course, both Expand and Select.

BillW