Summary of the use of the TreeView control in C#winform

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.