Getting the index of an Image from an ImageList - HOW?

  • Thread starter Thread starter Terry
  • Start date Start date
T

Terry

This seems like something that shouldn't be very difficult. But, how is one
supposed to get the index of an image in an image list from the reference of
the image?

I'm using VS .NET 2003 with .NET 1.1 and it says the "IndexOf()" method of
the ImageList.ImageCollection class is "Not supported" and does in fact
throw a "NotSupportedException" if I try to call it.

Since almost every control that uses an ImageList required the *index* of
the image, it seems kind of stupid to remove the "IndexOf" method. With
that gone, what are you supposed to do? Keep an index counter going as
images are added to the image list?
 
Hi,

The image list does not actually contain references to each image in the
list. I is actually a single image that is sub-divided, for example every 16
pixels is the start of the new image. So the 3rd image which will be at
index 2 starts at pixel 2*16 = 32 and is 16 pixels wide. Since there is not
a references to the original Image object that was added there is no easy
way to identify the image from the reference, unless you compare it pixel
for pixel against each sub-division in :).

Hope this helps

Chris Taylor
 
Hello,

Terry said:
But, how is one supposed to get the index of an image in an
image list from the reference of the image?

Why do you need that? The 'ImageList' class doesn't support that (that's
what documentation says).

HTH,
Herfried K. Wagner
 
You need the index of the image within the image list when adding a node to
a tree. The "Add()" method does not return an index either. So, if there's
no way to get an actual index that you can pass to the TreeNode constructor,
why does it require it?

[C#]
public TreeNode(
string text,
int imageIndex,
int selectedImageIndex
 
I understand that, but what am I supposed to pass to the "TreeNode"
constructor for the image index, if there is no way to get one?
 
Terry said:
You need the index of the image within the image list when adding a node to
a tree. The "Add()" method does not return an index either. So, if there's
no way to get an actual index that you can pass to the TreeNode constructor,
why does it require it?

Typically, you know the index because you create the
image list. What exactly are you trying to do?
 
Well, I'm not using an image strip and I have an unknown number of icons
that may go into the list. I am displaying nodes in the tree that can use
any icon specified by the user. When the program loads, I'm loading each of
the icons from the files and adding them to the image list. When the user
adds a node to the tree, I need to determine at run time what index
corresponds to the image I need to show.

It seems kind of short-sighted to assume that the programmer will always
know the index of an image in an image list. For now, I'm making the
assumption any image I add to the list is getting added at the end and that
the index will be "Count - 1". But, I don't feel comfortable with that.

The only other way I can think of to do this is to owner draw the control
and paint the image directly bypassing the whole ImageList thing.
 
Hi,

To use the images in a ImageList, you are required to set the ImageList
property of the TreeView to a existing ImageList.

You should have created the ImageList either by supplying a image strip or
adding the images one at a time with the Add method of the imagelists Images
property, in the later case the ImageList will build a image strip from the
images you added. The image index will be in the order that you added images
to the ImageList starting from 0.

Once you have given the TreeView control the ImageList, you can set the
ImageIndex property of the TreeNode to the relevant index from the
ImageList.

I hope this helps

Chris Taylor
 
It just seems so clunky to me considering how well thought out everything
else in .NET is. What I mean by that is, the implementation of the
ImageList.ImageCollection isn't consistant with other collections, i.e. it
"sort of" implements IList and ICollection, but not completely.

Also, it's a recipe for trouble putting the burden of index management on
the application. For example, if someone adds code somewhere that removes
an item from the ImageList but doesn't update all of the indexes
appropriately .... you get the idea. For static image strips I guess this
works fine, but I've got something more dynamic. I kind of expected the
"Add()" method to return an index (like the MFC CImageList.Add() method).
It is kind of curious that one of the three ImageList.Add methods *does*
return an int corresponding to the index of the image just added. The other
two Add methods do not.

Yes, I can keep track of the indexes but it just "feels like" I shouldn't
have to - the list itself should be able to tell me where something is.

At least I know now that it was intended to be like this - it's just
something I'll have to live with. I'll guess I'll be writing a subclass to
maintain indexes.

Thanks everyone for the replies. :-)
 
hello terry,

i get the feeling that you misinterpret the meaning of the imagelist -
better think of it as a designer which allows you to easily append
icons/ pictures-streams into containers (mainly forms) instead of
something which controls a collection of images.

indeed it just inserts a bitstream into the ressource-file; so if you
want to show all icons within your forms/ containers you have to open
up all these forms to receive that images....

probably better to think about developing a own Image-Manager for your
purpose i guess?

b. regs.
stefan
 
Back
Top