• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Referring to ImageList Images with a text string (like the Key property in VB6)

I'm new to VB.Net, trying to figure out equivalent ways of doing things. In VB I'm used to adding several icons to an ImageList and giving each Image a string for it's Key property. Then for TreeViews, ListViews, Toolbars etc, I used the syntax:

Me.ListView1.ListItems(1).SmallIcon = "GreenFolder"
Me.TreeView1.Nodes(1).Image = "GreenFolder"
Me.TreeView1.Nodes(1).ExpandedImage = "BlueFolder"
Me.Toolbar1.Buttons(1).Image = "GreenFolder"

Now I can't seem to find any way of referring to anything other than the Index number of the image with the syntax:

Me.TreeView1.Nodes(1).ImageIndex = 0

I find this method simply nausiating since I have to keep going back to the ImageList to look up the number of the image I want to use. I am amazed that in two hours of searching I haven't even found so much as a single comment about this issue on the net - I just hope that means the answer is so easy that everyone else found it straight away...
0
wwarby
Asked:
wwarby
  • 2
  • 2
1 Solution
 
purpleblobCommented:
Simply create Const variables i.e.

Private Const GreenFolder = 0

Now you've got the effeciency of numeric indexing (also less prone to errors, such as spelling mistakes) and you still have names for your image list items

Hope this helps
0
 
wwarbyAuthor Commented:
I see your point, but are you saying this is the only way? The trouble is, this isn't going to do me any favours if for example I want to populate the ImageList at runtime using file associated icons, and if I wanted to change the order of my icons in the ImageList or remove one in the middle, I'd have to change all my Constants.

Also, the problem seems to exist thoughout a number of controls - I now cannot use syntax like Me.TreeView1.Nodes("FirstNode").Expand or Me.ListView1.ListItems("FirstItem").Checked = True. I was hoping there is still a way to reference all these kinds of collection objects textually - if there isn't, how does anyone get around the problem of referring to a collection object that is added at runtime short of recording the Index property as a variable?
0
 
purpleblobCommented:
Okay let's take the first question (or first part) initially.

I'm certainly not aware that there's ay alternate indexing property into a listview or treeview - the ImageIndex gets/sets an int as you've found out.

Now to the second part of the first question (and I think probably answers your second question also) - if you filled the image list with assoociated files you could simply create a Hashtable using the icon name as the key and the image's index as the value - thus in essence you refer through the hashtable for the index for each image, but you can therefore reference the image by name. So this isn't too bad.

For example

Private association As Hashtable

association = New Hashtable

association("file1") = 0
association("mydoc") = 3
...

Now when you talk to the TreeView (for example) you can use the following:

treeView.Nodes(0).ImageIndex = association("file1")

With regards the indexes changing within your image list - ofcourse this is a problem as no unique identifier exists per image as they're now index based but I must admit I've never found this too much of a problem as I keep all my consts for such things together and it's usually fairly trivial to update them - but your point is well taken.



0
 
wwarbyAuthor Commented:
Ok, that makes sense. I had never heard of a HastTable before (second day in .NET) but it seems to work perfectly well. Found a way round my issue of reorganising images - since in this instance I am populating an ImageList at runtime from embedded icon resources, I'm using the following and repeating the statements for each icon:

ico16 = New System.Drawing.Icon(oAssembly.GetManifestResourceStream(Me.GetType, "FolderClosed.ico"), 16, 16)
Me.imlSmallIcons.Images.Add(ico16)
hashSmallIcons("FolderClosed") = Me.imlSmallIcons.Images.Count - 1

I suppose if I want to refer to a TreeView node by a string name I will have to apply the same concept. I appreciate it wasn't your decision to remove the Key property from all the controls but for my two cents an improvement this is not. What I could previously do with no code at all now takes a considerable amount of coding and it strikes me as a step backwards.
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.

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