ButtonBase.ImageIndex 属性

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Property ImageIndex As Integer
用法
Dim instance As ButtonBase
Dim value As Integer

value = instance.ImageIndex

instance.ImageIndex = value
[LocalizableAttribute(true)] 
public int ImageIndex { get; set; }
[LocalizableAttribute(true)] 
public:
property int ImageIndex {
    int get ();
    void set (int value);
}
/** @property */
public int get_ImageIndex ()

/** @property */
public void set_ImageIndex (int value)
public function get ImageIndex () : int

public function set ImageIndex (value : int)

属性值

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

异常

异常类型 条件

ArgumentOutOfRangeException

分配的值小于 ImageIndex 的下限。

备注

设置 ImageIndexImageList 属性后,Image 属性被设置为其默认值 空引用(在 Visual Basic 中为 Nothing)。

提示

如果 ImageList 属性值改为 空引用(在 Visual Basic 中为 Nothing),则 ImageIndex 属性返回它的默认值 -1。但是,将在内部保留已分配的 ImageIndex 值并在将另一个 ImageList 对象分配给 ImageList 属性时使用该值。如果分配给 ImageList 属性的新 ImageList 所具有的 ImageList.ImageCollection.Count 属性值小于或等于分配给 ImageIndex 属性的值减 1(由于集合的索引从零开始所致),则 ImageIndex 属性值将调整为比 Count 属性值小 1。例如,假设有一个按钮控件,它的 ImageList 有三个图像,并且它的 ImageIndex 属性设置为 2。如果分配给该按钮的新 ImageList 仅有两个图像,则 ImageIndex 值改为 1。

示例

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

Private Sub 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
End Sub 'AddMyImage
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;
 }
 
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;
   }
private void AddMyImage()
{
    // Assign an image to the ImageList.
    imageList1.get_Images().Add(Image.FromFile(
        "C:\\Graphics\\MyBitmap.bmp"));
    // Assign the ImageList to the button control.   
    button1.set_ImageList(imageList1);
    // Select the image from the ImageList (using the ImageIndex property).    
    button1.set_ImageIndex(0);
} //AddMyImage
private function AddMyImage()
 {
    // Assign an image to the ImageList.
    ImageList1 = new 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;
 }
 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ButtonBase 类
ButtonBase 成员
System.Windows.Forms 命名空间
ImageList