Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TreeNode image on expand-collapse events

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 ?

like image 876
Alexander Stalt Avatar asked Apr 02 '10 12:04

Alexander Stalt


People also ask

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.

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.


2 Answers

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.

like image 63
BillW Avatar answered Oct 04 '22 22:10

BillW


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

BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
like image 31
Matt Dearing Avatar answered Oct 04 '22 21:10

Matt Dearing