Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 125,968 times

Contents

Downloads

Related Categories

TreeView Control - Using Images

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • treeview

    Posted by jmhshep on 03 Mar 2005

    I tried your treeview example and when I try to add and image it is telling me that I need to initilaize the image first. Can you tell me what that means and how to fix it

  • treeview

    Posted by directorz on 28 May 2004

    I tried this and recieved the following: Compile error: variable not defined..."TreeView" is highlited...anyone have any ideas?

  • Good & Thanks

    Posted by gabar on 14 May 2004

    Very Good, You have helped me.

    gabriel medina
    gxsoft@hotmail.com

  • Good

    Posted by behmagvb on 11 Aug 2003

    Good Example isn't it?
    http://www.pldental.com

  • ??? Got Problems with that

    Posted by LittleMik on 24 Jun 2003

    Hi,

    Found this very good tutorial because its nearly exact the problem i have with my TreeView,...
    ...but unfortunatly, i can't get it to work...

    Set the same settings like in the tutorial, but...