Export (0) Print
Expand All

ButtonBase.ImageIndex Property

Gets or sets the image list index value of the image displayed on the button control.

[Visual Basic]
Public Property ImageIndex As Integer
[C#]
public int ImageIndex {get; set;}
[C++]
public: __property int get_ImageIndex();
public: __property void set_ImageIndex(int);
[JScript]
public function get ImageIndex() : int;
public function set ImageIndex(int);

Property Value

A zero-based index, which represents the image position in an ImageList. The default is -1.

Exceptions

Exception Type Condition
ArgumentException The assigned value is less than the lower bounds of the ImageIndex.

Remarks

When the ImageIndex or ImageList properties are set, the Image property is set to its default value, a null reference (Nothing in Visual Basic).

Note   If the ImageList property value is changed to a null reference (Nothing), the ImageIndex property returns its default value, -1. However, the assigned ImageIndex value is retained internally and used when another ImageList object is assigned to the ImageList property. If the new ImageList assigned to the ImageList property has an 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. For example, consider a button control whose ImageList has three images and whose ImageIndex property is set to 2. If a new ImageList that has only two images is assigned to the button, the ImageIndex value changes to 1.

Example

The following example uses the derived class, Button and sets the ImageList and ImageIndex properties. This code assumes an ImageList has been created and a minimum of one Image has been assigned to it. This code assumes you have a bitmap image named MyBitMap.bmp stored in the C:\Graphics directory.

[Visual Basic] 
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

[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;
 }
 

[C++] 
private:
    void AddMyImage() {
    // Assign an image to the imageList.
    imageList1->Images->Add(Image::FromFile(S"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;
    }

[JScript] 
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;
 }
 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ButtonBase Class | ButtonBase Members | System.Windows.Forms Namespace | ImageList

Show:
© 2014 Microsoft