Domanda

I am beginner in c#. This is a small question. I want to add Images to image list with image index.

  ilsLargeFlags  --> ImageList
  strFlagPath --> full file path
  strPetPath  --> only file name (gif image)

I tried the below code: (not working)

 ilsLargeFlags.Images.Add(Image.FromFile(strFlagPath));
 ilsLargeFlags.Images.SetKeyName(8, strPetPath);

what I am doing wrong in the above code?

There are already 8 images in the imageList. added by simple user interface( i dont know what it is said to be)

È stato utile?

Soluzione

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().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top