如何使用imageList控件
我有一些图像,我手动添加到imageList Cotrol。 现在我需要从imageList中删除thart图像,具体取决于键索引并设置为面板背景。
我该怎么办
您在图像列表中添加的图像将添加到ImageList.ImageCollection中 ,因此它是集合类型,然后您可以使用大多数集合方法。
使用Images属性添加,删除和访问要在面板背景中显示的图像。 添加(键,图像)
去掉()
RemoveAt移除()
RemoveByKey()
检查ImageList类文档中的示例,以了解如何实际使用所有这些方法。
添加图片:
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"];
使用ImageList
控件的Images
属性 。
它返回的ImageList.ImageCollection
对象提供了操作列表中图像所需的所有方法,包括Add
和Remove
方法。
您可以在此处找到有关设置Panel
控件背景的说明: 如何:设置Windows窗体面板的背景
我在列表视图中使用imagelist。
假设我在图像列表中有三个图像,并希望删除其中的2个图像。
我用过代码
imagelist.Images.RemoveAt(2);
代码正在删除第二张图片,但之后3张图片是机器人可见,虽然它在那里