views:

617

answers:

4

I have a treeView with many nodes. I want that some nodes change their image when node collapsed/expanded. How can I do it ?

Unfortunately, TreeNode don't have properties like ExpandNodeImage, CollapseNodeImage \

TreeView can change very often, so nodes can be deleted/added.. i can delete child nodes and so on...

Maybe, there is a class like ExpandAndCollapseNode ?

A: 

you can use the events AfterCollapse & AfterExpand (that are avilable on the TreeView itself) to modify the image of a node.

you can get the node using the TreeViewEventArgs input parameter:

private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
    e.Node.ImageIndex = 1;
}
Ami
+1  A: 

TreeViews have the following events that will be be fired when nodes are collapsed/expaned.

BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
Matt Dearing
+1  A: 

1). Add an ImageList Control to your WinForm.

2). Populate the ImageList with the pictures/icons you wish to change/display in response to what the user does at run-time with the TreeView, such as expanding, or collapsing nodes.

3). Assign the 'ImageList Control to the 'ImageList property of the 'TreeView

At this point you may want to make an initial pass over the TreeView, assuming it is populated, assigning the Node.ImageIndex property to point to the Image ... in the ImageList ... you want to use for the Node depending on whether it has children, or whatever.

4). If a user expands a Node, for example, you can use the BeforeExpand Event of the TreeView to change the Node's picture : like so : in this case we use the index of the Picture in the ImageList :

    private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        e.Node.ImageIndex = 3;
    }

5) You can also set the Node's image by using the ImageKey property which is the string name of the Image

6) There lots of other possible Node Picture variations to use : check out : SelectedImageIndex and SelectedImageKey : You can change Node pictures in the BeforeSelect, AfterSelect and BeforeExpand, events also, depending on the effect you are after.

BillW
It's strange for me, that this functionality contains only at treeview level, not at treenode level.
Alexander Stalt
@Alexander Not quite sure I fully understand your comment. All these Events that are Node related (BeforeExpand, AfterExpand, BeforeCollapse, AfterCollapse, BeforeSelect ... and on and on) really are functions related to activity with/on/at/to TreeNodes, but they are defined as TreeView Events in the same consistent way that other "events of child components" of Controls in standard WinForms are defined as Control Events. I'm trying to imagine what it would mean if these Events were at the "TreeNode" level : would that mean each TreeNode could have a separate Event Handler ? best,
BillW
I would also sometimes create an enum that would have a description of the image so you don't have ImageIndex = X running around in your code. That way you have something a bit more readable so when you come back to it you have an inkling of what's going on.
Justin
@Justin Curious to ask you: do you think using an Enum would be more descriptive than naming the image/icon descriptively, and then using the 'ImageKey property to set the image/icon, rather than 'ImageIndex ? You may be about to teach me a new way of looking at the use of 'Enums, and I'm happy to hear your thoughts !
BillW
@BillW I think either way is an OK way to do it. For some reason I've always used the ImageIndex rather than the ImageKey. Maybe it's because I'm an old C++ programmer and when it comes to performance I will always pick the integer over the string. Enums in C# are pretty much just a cover for an integer so I use them a lot.
Justin
BillW, ok, i will try to explain what I mean.For example, I have a treeview with different type of nodes. Each node have different images in expanded or collapsed state. Would it be easier to determine "expand event" or "collapse event" in treenode class ? Or for example to determine Property like ExpandImage or CollapseImage ? Why do I need to subscribe "before expand", "middle of expand", "after expand", "1 seconds after expand" events ? All I need is to change an image of treenode. Why do I need to use events for that ? Why don't treenode have a CollapseImageIndex, ExpandImageIndex ?
Alexander Stalt
A: 

It's better to use..

yourtreeNode.SelectedImageIndex = 1;

Ajay Bisht