1. How to change the icon when the directory is expanded (note: not selected)
To use an icon in a table of contents, first add a control ImageList (named ImageList1), and then you can refer to the picture by its index or name.
You then need to point to imageList1 in the ImageList property of the TreeView control.
The TreeView has two properties:
Selectimageindex: The index of the picture that is displayed when the node is selected
ImageIndex: The index of the picture that is displayed when the node is not selected
The effect that can be achieved is that the picture of the node is changed when a node is selected, and if our goal is the same, everything is already in order.
But what I want to do is change the picture of that node when you expand a node (as shown in the Open box), and change the picture of the node when it collapses (such as a packaged box). Using properties directly does not enable the effect.
The implementation principle is: When you expand a node, Selectimageindex and ImageIndex all point to the picture of the Open box
Selectimageindex and ImageIndex all point to the picture of the box when collapsing a node
Nature requires two events: the Afterexpand and Aftercollapse events of the TreeView
Private voidTreeview1_afterexpand (Objectsender, TreeViewEventArgs e) {E.node.imageindex=1;//point to expanded iconE.node.selectedimageindex =1;//point to expanded icon } Private voidTreeview1_aftercollapse (Objectsender, TreeViewEventArgs e) {E.node.imageindex=0;//point to close iconE.node.selectedimageindex =0;//point to close icon}
PS: When a property on a node
ImageIndex and Once the selectedImageIndex is assigned, the child nodes that are added to it, such as the attribute is not specified, seem to follow the parent node's properties, so I usually set the value separately for the child node.
Summary of the use of the TreeView control in C#winform