Link to home
Start Free TrialLog in
Avatar of wwarby
wwarby

asked on

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...
Avatar of purpleblob
purpleblob

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
Avatar of wwarby

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of purpleblob
purpleblob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wwarby

ASKER

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.