TreeView Control

Using Images

 

Like the listview control, the treeview control needs to be 'bound' to an image control. To do this, add an ImageList Control to your form, name it, and add any images you want to use in the TreeView control to it. Be sure to set the image size first (normally set to 16x16). Set keys if you wish, as this makes it easier to identify each image. It is best to stick with one case system (ie all caps, all lowercase etc).

Now, change the ImageList property of the TreeView control to the name of the ImageList you want to use. Now, when you add a new node to the control, you can specify and image index or key too. That image will then be displayed next to the node. The following code adds a new node, with the image "TextFile" displayed next to it.

TreeView1.Nodes.Add , , , "TestImageItem", "TextFile"

You can also specify a image to be displayed when the item is selected . The following code adds a new node. The image "FolderClosed" will be displayed, and when it is selected, "FolderOpen" will be displayed (like the folders in explorer).

TreeView1.Nodes.Add , , , "TestFolderItem", "FolderClosed", "FolderOpen"

Note that in place of the text used to identify the image, you can also use its index:

TreeView1.Nodes.Add , , , "TestImageItem", 1

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”