简体   繁体   English

如何使用图像索引将图像添加到imageList

[英]How to add images to imageList with image Index

I am beginner in c#. 我是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. imageList中已经有8张图像。 added by simple user interface( i dont know what it is said to be) 通过简单的用户界面添加(我不知道这是什么)

You should better call ImageList.ImageCollection.Add(String, Image) . 您最好调用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: 通过使用ImageList及其内部的ImageListCollection您将无法设置特定的数字索引(因此,它不支持IndexOf()方法。而是使用某种自定义的字符串键。如果确实需要当前索引,在图像上,您应该在将其添加到具有所需键的列表中之后使用IndexOfKey()方法:

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. 现在可以使用SetKeyName()方法和检索到的索引来更改键名,但是如果有人要从列表中删除图像,则这可能导致键名加倍,然后再添加另一个图像。 So better stick to some key names and get out the corresponding index at the moment you'll need it by calling IndexOfKey() . 因此最好坚持使用一些键名,并在需要时通过调用IndexOfKey()获得相应的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM