You should better call ImageList.ImageCollection.Add(String, Image)
. In that case you could provide the key name in one step:
ilsLargeFlags.Images.Add(strPetPath, Image.FromFile(strFlagPath));
Update
By using the ImageList
and its internal ImageListCollection
you won't be able to set a specific numeric index (hence it does not support IndexOf()
method. Instead you use some kind of self-defined string key. If you really need the current index of on image you should use the IndexOfKey()
method after adding it to the list with the desired key:
ilsLargeFlags.Images.Add(strPetPath, Image.FromFile(strFlagPath));
var index = ilsLargeFlags.Images.IndexOfKey(strPetPath);
Now it would be possible to change the key name by using the SetKeyName()
method and the retrieved index, but this could lead to doubled key names if someone is going to remove images from the list and afterwards you would add another image. So better stick to some key names and get out the corresponding index at the moment you'll need it by calling IndexOfKey()
.