ButtonBase.ImageIndex 屬性

定義

取得或設定按鈕控制項所顯示影像的影像清單索引值。Gets or sets the image list index value of the image displayed on the button control.

public:
 property int ImageIndex { int get(); void set(int value); };
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter))]
public int ImageIndex { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter))>]
member this.ImageIndex : int with get, set
Public Property ImageIndex As Integer

屬性值

Int32

以零起始的索引,表示 ImageList 中的影像位置。A zero-based index, which represents the image position in an ImageList. 預設值為 -1。The default is -1.

屬性

例外狀況

value 小於 -1。value is less than -1.

範例

下列程式碼範例會使用衍生類別, Button 並設定 ImageListImageIndex 屬性。The following code example uses the derived class, Button and sets the ImageList and ImageIndex properties. 這段程式碼需要 ImageList 建立,而且至少有一個指派給 Image 它。This code requires that an ImageList has been created and a minimum of one Image has been assigned to it. 這段程式碼也會要求您在目錄中有一個名為「儲存」的點陣圖影像 MyBitMap.bmp C:\GraphicsThis code also requires that you have a bitmap image named MyBitMap.bmp stored in the C:\Graphics directory.

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 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

備註

ImageIndex 設定或 ImageList 屬性時, Image 屬性會設定為其預設值 nullWhen the ImageIndex or ImageList properties are set, the Image property is set to its default value, null.

ImageKeyImageIndex 都是互斥的,也就是說,如果設定了一個,另一個會設定為不正確值並予以忽略。ImageKey and ImageIndex are mutually exclusive, meaning if one is set, the other is set to an invalid value and ignored. 如果您設定 ImageKey 屬性, ImageIndex 屬性會自動設定為-1。If you set the ImageKey property, the ImageIndex property is automatically set to -1. 或者,如果您設定 ImageIndex 屬性,會 ImageKey 自動將設定為空字串 ( "" ) 。Alternatively, if you set the ImageIndex property, the ImageKey is automatically set to an empty string ("").

如果 ImageList 屬性值變更為 null ,屬性會傳回 ImageIndex 其預設值-1。If the ImageList property value is changed to null, the ImageIndex property returns its default value, -1. 但是,指派的 ImageIndex 值會在內部保留,並在另一個 ImageList 物件指派給 ImageList 屬性時使用。However, the assigned ImageIndex value is retained internally and used when another ImageList object is assigned to the ImageList property. 如果指派給屬性的新屬性 ImageList ImageList ImageList.ImageCollection.Count 值的屬性值小於或等於指派給屬性的值,則會將該集合的值 ImageIndex 減一 (,以將集合視為以零為基底的索引) ,則 ImageIndex 會將屬性值調整為小於屬性值的一個值 CountIf the new ImageList assigned to the ImageList property has an ImageList.ImageCollection.Count property value that is less than or equal to the value assigned to the ImageIndex property minus one (to account for the collection being a zero-based index), the ImageIndex property value is adjusted to one less than the Count property value. 例如,假設有一個按鈕控制項,其 ImageList 有三個影像,而其 ImageIndex 屬性設定為2。For example, consider a button control whose ImageList has three images and whose ImageIndex property is set to 2. 如果將 ImageList 只有兩個影像的新指派給按鈕,此值會 ImageIndex 變更為1。If a new ImageList that has only two images is assigned to the button, the ImageIndex value changes to 1.

適用於

另請參閱