0

I want to create a listView with remove buttons like this in WinForms:

enter image description here

After some searching, it seems like ObjectListView is the way to go. After much struggling, I've got text to show up for my items, but I can't get the icons to show up. I've tried going the ImageAspectName route and the ImageGetter route - no success. Here's what I've got for some sample code:

My class:

public class item
{
    public string Title { get; set; }
    public Image RemoveButton { get; set; }
}

And the code for putting the items in the ObjectListView:

item listItem1 = new item();
listItem1.Title = "this is a test title";
listItem1.RemoveButton = Properties.Resources.Remove;
List<item> listItems = new List<item>() { listItem1 };
objectListView1.SetObjects(listItems);
olvColumn2.ImageGetter += delegate (object rowObject) { return ((item)rowObject).RemoveButton; };

My first question is: can I just use the Properties.Resources.Remove like I've got above? What should the Build Action be for it? Just "Resource"?

EDIT:

If I try to use objectListView1.SmallImageList then it throws an exception (IndexOutOfRangeException):

ImageList imageList = new ImageList();
imageList.Images.Add(Properties.Resources.Remove);
objectListView1.SmallImageList = imageList;

I have also tried using a different size of the same image (16x16 instead of 256x256) but to no avail.

Here is my simple test project in case anyone wants to tell me what I'm doing wrong: https://drive.google.com/uc?export=download&id=0B_-6KdFaC6WiMmMxU0JaUktUeWs

derekantrican
  • 1,891
  • 3
  • 27
  • 57
  • Possible duplicate of [ObjectListView - Delete a row by clicking on a designated column with fixed content/text](https://stackoverflow.com/questions/12629417/objectlistview-delete-a-row-by-clicking-on-a-designated-column-with-fixed-cont) – Rev Jun 29 '17 at 07:17
  • @Rev1.0 I see how that looks like a duplicate (my question and that question both want the same end goal), but their question is just a general "how do I do this" whereas I'm struggling with getting images to show up (Please reread the questions at the end of my post) – derekantrican Jun 29 '17 at 12:57
  • Assigning the image from the Resources should work that way. Did you set the "OwnerDraw" property on the OLV? IIRC its required to display images. – Rev Jun 29 '17 at 13:42
  • Are you sure you want the remove button be part of you data object? Why? Isn't it the same for all rows? Its seems odd. – Rev Jun 29 '17 at 13:43
  • Yes, I have set the OwnerDraw property to true. The button will be the same for all rows - is there a better way to set it, then? – derekantrican Jun 29 '17 at 13:44
  • Then you should probably do it the way its suggested in the linked answer. Add an SmallImageList with the Image and assign it to the OLV. Then return the Image by returning the image index. – Rev Jun 29 '17 at 13:50
  • Did you set ShowImagesOnSubItems to true? – Rev Jun 29 '17 at 13:52
  • Yes, I had set that to true. Also, see my updated question in regards to SmallImageList – derekantrican Jun 29 '17 at 13:59
  • Did you return index 0 from the ImageGetter? – Rev Jun 29 '17 at 14:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147943/discussion-between-derekantrican-and-rev1-0). – derekantrican Jun 29 '17 at 14:07
  • Did you solve it by changing the image size? – Rev Jun 30 '17 at 07:25
  • @Rev1.0 Unfortunately, no – derekantrican Jun 30 '17 at 12:59

0 Answers0