Search code examples
vb.netimageobjectlistview

Is It Possible To Use Embedded Resource Images For ObjectListView SubItems


I can't seem to find any information on this, but does anyone know if it is possible to use an embedded image from my.resources within an ObjectListView SubItem?

I am currently using images stored in an ImageList control, but I find for some reason, those images get corrupted and start displaying imperfections. I don't have the problem if I pull them from the embedded resources as I previously have with the normal listview control.

I am using the ImageGetter routine to return a key reference the ImageList, however, ultimately I would like to pull these from embedded resources. I would like to also do this for animated GIF references as well.

If anyone knows a way, could you please assist. Sample code would be appreciated. Thanks


Solution

  • You can return three different types from the ImageGetter

    1. int - the int value will be used as an index into the image list
    2. String - the string value will be used as a key into the image list
    3. Image - the Image will be drawn directly (only in OwnerDrawn mode)

    So option number 3 could be what you want. Note that you have to set objectListView1.OwnerDraw = true.

    If that does not work, an alternative could be to load the images into the ImageList at runtime. There is an example here.

    this.mainColumn.ImageGetter = delegate(object row) {
        String key = this.GetImageKey(row);
        if (!this.listView.LargeImageList.Images.ContainsKey(key)) {
            Image smallImage = this.GetSmallImageFromStorage(key);
            Image largeImage = this.GetLargeImageFromStorage(key);
            this.listView.SmallImageList.Images.Add(key, smallImage);
            this.listView.LargeImageList.Images.Add(key, largeImage);
        }
        return key;
    };
    

    This dynamically fetches the images if they haven’t been already fetched. You will need to write the GetImageKey(), GetSmallImageFromStorage() and GetLargeImageFromStorage() methods. Their names will probably be different, depending on exactly how you are deciding which image is shown against which model object.