ButtonBase.ImageList Property

Definition

Gets or sets the ImageList that contains the Image displayed on a button control.

C#
public System.Windows.Forms.ImageList ImageList { get; set; }
C#
public System.Windows.Forms.ImageList? ImageList { get; set; }

Property Value

An ImageList. The default value is null.

Examples

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

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

Remarks

When the ImageList or ImageIndex property is set, the Image property is set null, which is its default value.

Note

If the ImageList property value is changed to null, the ImageIndex property returns its default value, -1. However, the assigned ImageIndex value is retained internally and used when another ImageList is assigned to the ImageList property. If 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 (because the collection is 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.

Applies to

Product Versions
.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

See also