Changing TreeNode Image in Failure Events

I have a treeView with many nodes. I want some nodes to change their image when the node is minimized / expanded. How can i do this?

Unfortunately, TreeNode does not have properties such as ExpandNodeImage, CollapseNodeImage \

TreeView can change very often, so nodes can be deleted / added. I can delete child nodes, etc.

Maybe there is a class like ExpandAndCollapseNode?

+4
source share
5 answers

1). Add an ImageList control to your WinForm.

2). Fill the ImageList with the images / icons you want to change / display in response to what the user does at runtime using the TreeView, for example, expanding or collapsing nodes.

3). Assign "ImageList Control" to the "ImageList" Property in TreeView

At this point, you can make an initial pass over the TreeView, assuming it is full, by setting the Node.ImageIndex property to point to Image ... in the ImageList ... that you want to use for Node depending on whether it has children or something else.

4). For example, if a user extends Node, you can use the BeforeExpand TreeView event to change the Node image: like this: in this case we use the image index in ImageList:

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

5) You can also set the Node image using the ImageKey property, which is the string name Image

6) There can be many other possible Node image options: check: SelectedImageIndex and SelectedImageKey: you can change the Node of the image in the BeforeSelect, AfterSelect and BeforeExpand events, depending on the effect you are using.

+8
source

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

 BeforeCollapse BeforeExpand AfterCollapse AfterExpand 
+2
source

Better to use:

 treeNode.SelectedImageIndex = 1; 
+2
source

you can use the AfterCollapse and AfterExpand events (which are available for the TreeView itself) to modify the node image.

you can get the node using the TreeViewEventArgs input parameter:

 private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e) { e.Node.ImageIndex = 1; } 
0
source
 BeforeCollapse BeforeExpand AfterCollapse AfterExpand 

Use both ImageIndex and SelectedImageIndex:

 private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) { e.Node.ImageIndex = 1; e.Node.SelectedImageIndex = 1; } 
0
source

Source: https://habr.com/ru/post/1305843/


All Articles