Solved

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

Posted on 2003-11-02
4
256 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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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