[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2003-11-02
4
Medium Priority
?
266 Views
Last Modified: 2008-02-01
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
Comment
Question by:wwarby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:purpleblob
ID: 9665492
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
 
LVL 1

Author Comment

by:wwarby
ID: 9665513
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
 
LVL 6

Accepted Solution

by:
purpleblob earned 1000 total points
ID: 9665569
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
 
LVL 1

Author Comment

by:wwarby
ID: 9665609
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question