There are two things you need to do
- Add image to ImageList if it isn't already in it
- Create new ListViewItem and assign image from previous point to it
It could go like this, based on your code:
// Add markerIcon to ImageList under Entrykey
imagelistforTypeIcons.Images.Add(Entrykey, message.marker.markerIcon);
// Use icon from ImageList which is stored under Entrykey
ListViewItem row = new ListViewItem(rowEntry);
row.ImageKey = Entrykey;
// Do whatever else you need afterwards
row.Tag = Entrykey;
....
Problem with code in your question (without actually trying it out) looks to be in ImageIndex
you are assigning.
- you are adding new image to one image list, but assigning an image to
ListViewRow
from a different one - you are providing image index in constructor but setting it to 0 afterwards (why?)
- you are providing wrong image index in the first place, because you calculated index of last image in the image list, before adding the new image.
So your code could also be fine like this:
// Add markerIcon to ImageList under Entrykey
imagelistforTypeIcons.Images.Add(Entrykey, message.marker.markerIcon);
// Use icon from ImageList which is stored under Entrykey
ListViewItem row = new ListViewItem(rowEntry);
row.ImageIndex = imagelistforTypeIcons.Items.Count - 1;
// Do whatever else you need afterwards
row.Tag = Entrykey;