ButtonBase.ImageIndex 属性

定义

获取或设置按钮控件上显示的图像的图像列表索引值。

C#
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter))]
public int ImageIndex { get; set; }

属性值

从零开始的索引,表示 ImageList 中的图像位置。 默认值为 -1。

属性

例外

value 小于 -1。

示例

下面的代码示例使用派生类, Button 并设置 ImageListImageIndex 属性。 此代码要求 ImageList 已创建 ,并且至少为其分配了一个 Image 。 此代码还要求将名为 的 MyBitMap.bmp 位图图像存储在 目录中 C:\Graphics

C#
private void AddMyImage()
 {
    // Assign an image to the ImageList.
    ImageList1.Images.Add(Image.FromFile("C:\\Graphics\\MyBitmap.bmp"));
    // Assign the ImageList to the button control.   
    button1.ImageList = ImageList1;
    // Select the image from the ImageList (using the ImageIndex property).    
    button1.ImageIndex = 0;
 }

注解

ImageIndex设置 或 ImageList 属性后,属性Image将设置为其默认值 null

ImageKeyImageIndex 是互斥的,这意味着如果设置了一个,另一个设置为无效值并忽略。 如果设置 ImageKey 属性,该 ImageIndex 属性将自动设置为 -1。 或者,如果设置 ImageIndex 属性,则 ImageKey 会自动设置为空字符串 (“”) 。

如果属性值 ImageList 更改为 null,该 ImageIndex 属性将返回其默认值 -1。 但是,分配 ImageIndex 的值在内部保留,并在将另一个 ImageList 对象分配给属性 ImageList 时使用。 如果分配给该属性的新 ImageList 属性值小于或等于分配给ImageIndex该属性的值减去 1 (以解释集合为从零开始的索引) ,则ImageIndex属性值将调整为小于属性值的 CountImageList.ImageCollection.Count 1。ImageList 例如,假设有一个按钮控件,该 ImageList 控件具有三个图像,其 ImageIndex 属性设置为 2。 如果将只有两个图像的新 ImageList 分配给该按钮,该值 ImageIndex 将更改为 1。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅