Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

VB.net TreeView adding images by name from resources

Hi

I have images in my project resources  that I want to add tol a TreeView
If the resource files are called RED.png BLUE.png and YELLOW.png

How would I add these to certain TreeView nodes
0
Murray Brown
Asked:
Murray Brown
1 Solution
 
CodeCruiserCommented:
You need to use the ImageList as you have been. Add resources to image list and then use its index in treeview.
0
 
Rose BabuSenior Team ManagerCommented:
Hi,

assume the below tree structure
- Root
      - Red
      - Blue
      - Yellow

and set the images to the treenodes as like below in your code
'create an ImageList and add the images from the resource files
Dim tvImageList As New ImageList
tvImageList.Images.Add(My.Resources.Red) ' Red.png
tvImageList.Images.Add(My.Resources.Blue) ' Blue.png
tvImageList.Images.Add(My.Resources.Yellow) ' Yellow.png

' Set the imagelist to the treeview
mnuTreeView.ImageList = tvImageList

mnuTreeView.ExpandAll()

'nodRoot - Root Node's name
'nodRed - Red Node's name
'nodBlue - Blue Node's name
'nodYellow - Yellow Node's name

' set the ImageIndex value for the nodes you wanted like below
mnuTreeView.Nodes("nodRoot").ImageIndex = -1 : mnuTreeView.Nodes("nodRoot").SelectedImageIndex = -1

mnuTreeView.Nodes("nodRoot").Nodes("nodRed").ImageIndex = 0 : mnuTreeView.Nodes("nodRoot").Nodes("nodRed").SelectedImageIndex = 0
mnuTreeView.Nodes("nodRoot").Nodes("nodBlue").ImageIndex = 1 : mnuTreeView.Nodes("nodRoot").Nodes("nodBlue").SelectedImageIndex = 1
mnuTreeView.Nodes("nodRoot").Nodes("nodYellow").ImageIndex = 2 : mnuTreeView.Nodes("nodRoot").Nodes("nodYellow").SelectedImageIndex = 2

Open in new window

Try this. It may help you.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now