0

I'm using the ObjectListViewand am trying to add images to my items. I got it to work by looping through all the items and then manually editing the image index per item. I would like to know if this is possible when adding the items. This is the code I have at the moment:

Adding the items

for (int i = 0; i < listName.Count; i++)
{
    games newObject = new games(listName[i], "?");
    lstvwGames.AddObject(newObject);
}

Adding the images

foreach (string icon in listIcon)
{
    imglstGames.Images.Add(LoadImage(icon)); // Download, then convert to bitmap
}
for (int i = 0; i < lstvwGames.Items.Count; i++)
{
    ListViewItem item = lstvwGames.Items[i];
    item.ImageIndex = i;
}
Yuki Kutsuya
  • 3,968
  • 11
  • 46
  • 63
  • Its not entirely clear to me what you want to achieve, but your approach seems wrong. Do you just want to show a different image in the first column for every row object? Are the images available at compile time or do you need to fetch them an runtime (LoadImage())? – Rev Aug 26 '14 at 06:59
  • @Rev1.0 The images are being downloaded and put into a list before I call this function. I want to add the items to the `ObjectListView` but I can't seem to give the items an image index because I have to add `objects` instead of `listviewitems`. – Yuki Kutsuya Aug 26 '14 at 08:02
  • Do you want to use a different image for each item/object? – Rev Aug 26 '14 at 08:21

2 Answers2

8

It is not entirely clear to me what exactly you try to achieve, but there are several ways to "assign" an image to a row. Note that you probably have to set

myOlv.OwnerDraw = true;

which can also be set from the designer.

If you have a specific image for each of your model objects, its probably best to assign that image directly to your object and make it accessible through a property (myObject.Image for example). Then you can use the ImageAspectName property of any row to specify that property name and the OLV should fetch the image from there.

myColumn.ImageAspectName = "Image";

Another way to do it is using the ImageGetter of a row. This is more efficient if several of your objects use the same image, because you can fetch the image from anywhere you want or even use the assigned ImageList from the OLV by just returning an index.

indexColumn.ImageGetter += delegate(object rowObject) {
    // this would essentially be the same as using the ImageAspectName
    return ((Item)rowObject).Image;
};

As pointed out, the ImageGetter can also return an index with respect to the ObjectListView's assigned ImageList:

indexColumn.ImageGetter += delegate(object rowObject) {
    int imageListIndex = 0;

    // some logic here
    // decide which image to use based on rowObject properties or any other criteria

    return imageListIndex;
};

This would be the way to reuse images for multiple objects.

Rev
  • 5,827
  • 4
  • 27
  • 51
1

Both your approach and the one I show below will have problems if the list is ever sorted as sorting will change the order of the objects in the list. But really all you have to do is keep track of your object count in your foreach loop.

int Count = 0;
foreach (string icon in listIcon)
{
    var LoadedImage = LoadImage(icon);
    LoadedImage.ImageIndex = Count;
    imglstGames.Images.Add(LoadedImage); // Download, then convert to bitmap
    Count++;
}
HodlDwon
  • 1,131
  • 1
  • 13
  • 30
  • The icon itself isn't a ListViewItem so I can't assign an image index to it :(. – Yuki Kutsuya Aug 25 '14 at 23:38
  • this class appears to be part of a third-party library, have you looked at their documentation for the `Add` method? You could possibly overload it or maybe they already have an alternative method? – HodlDwon Aug 26 '14 at 00:09
  • I have looked at their documentation, but I couldn't find anything. That's why I posted here on Stackoverflow. – Yuki Kutsuya Aug 26 '14 at 00:09
  • http://objectlistview.sourceforge.net/cs/gettingStarted.html#mental-gear-shift it speaks of configuration... start with that? maybe you don't need to sort or add index values at all? no offense, but you might be using it "wrong", by that I mean in a way that the designer did not intend... – HodlDwon Aug 26 '14 at 00:14
  • @FoxyShadoww: JoshW is correct that you approach is flawed. For instance, you never work with the underlying ListViewItem objects because the ObjectListView intentionally add a layer of abstraction and only uses them internally. – Rev Aug 26 '14 at 06:57