How to add images in vb.net treeview+vb.net 2005 windows programming

Hi,
Any suggestions on how do I add images to a treeview.And also on how images change when tree node is expanded or closed.

Cheers
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
Use an ImageList component. Bind the ImageList to the treeview by setting the ImageList property of the Treeview. Of course, the images are set inside the ImageList control.

Each node of the treeview has 4 properties:
ImageIndex
ImageKey
SelectedImageIndex
SelectedImageKey
... that determines which image of the ImageList contents shows the node. You can set 1 global image (ImageIndex/ImageKey) and 1 for when the node is selected (SelectedImageIndex/SelectedImageKey).

Hope that helps.
RIASAuthor Commented:
Is it necessary to set SelectedImageKey?

Luis PérezSoftware Architect in .NetCommented:
No, if you don't set it then the ImageKey is used for both modes (selected / unselected). In fact, where you add nodes to the treeview, several of the many constructors of Treeview.Nodes.Add function admits directly an ImageIndex (but none of them admits SelectedImageIndex, if I remember it well).
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

RIASAuthor Commented:
Cheers mate will give a try
RIASAuthor Commented:
Hi,
The images are fine but still can't expanded and closed nodes images to change
Luis PérezSoftware Architect in .NetCommented:
Well, the node has ImageIndex and SelectedImageIndex. This means that each node admits 2 images, one for selected and one for not selected. The selected image is not necessary.

But "selected" is not the same as "expanded". You can expand a node and not select it. If you want to change the node image specifically when it expands/collapse, the you must to do programatically, using the "AfterExpand" and "AfterCollapse" events:

    Private Sub TreeView1_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterExpand
        e.Node.ImageIndex = [index_of_the_image_you_want_when_node_is_expanded]
    End Sub

    Private Sub TreeView1_AfterCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCollapse
        e.Node.ImageIndex = [index_of_the_image_you_want_when_node_is_collapsed]
    End Sub

Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
Cheers mate
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.