如何使用imageList控件

时间:2011-12-21 09:02:37

标签: c# .net winforms imagelist

我有一些图像,我手动添加到imageList控件。 现在我需要从imageList中删除thart图像,具体取决于键索引并设置为面板背景。我该怎么做

3 个答案:

答案 0 :(得分:16)

您在图像列表中添加的

Images已添加到ImageList.ImageCollection,因此它是集合类型,然后您可以使用大多数集合方法。

使用Images属性添加,删除和访问要在面板背景中显示的图像。 Add(key,image)
Remove()
RemoveAt()
RemoveByKey()

检查ImageList Class文档中的示例,以了解如何实用地使用所有这些方法。

添加图片

imageList1.Images.Add("pic1", Image.FromFile("c:\\mypic.jpg"));

从集合中删除图片:

imageList1.Images.RemoveAt(listBox1.SelectedIndex);
imageList1.Images..RemoveByKey("pic1");

要访问图像,请从imagecollection中获取图像

panel1.BackgroundImage = imageList1.Images[0];

panel1.BackgroundImage = imageList1.Images["pic1"];

答案 1 :(得分:1)

使用ImageList控件的Images property

它返回的ImageList.ImageCollection对象提供了操作列表中图像所需的所有方法,包括AddRemove方法。

您可以在此处找到有关设置Panel控件背景的说明:How to: Set the Background of a Windows Forms Panel

答案 2 :(得分:0)

我在列表视图中使用imagelist。

假设我在图像列表中有三个图像,并且想要删除其中的2个图像。

我使用了代码

    imagelist.Images.RemoveAt(2);

代码正在删除第二张图片,但之后3张图片是机器人可见的,虽然它在那里

相关问题