Solved

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

Posted on 2003-11-02
4
247 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now