次の方法で共有


ButtonBase.ImageIndex プロパティ

ボタン コントロールに表示されているイメージのイメージ リスト内でのインデックス値を取得または設定します。

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

プロパティ値

ImageList 内でのイメージの位置を表す 0 から始まるインデックス。既定値は -1 です。

例外

例外の種類 条件
ArgumentException 代入された値が ImageIndex の下限値より小さいです。

解説

ImageIndex プロパティまたは ImageList プロパティが設定されている場合は、 Image プロパティが既定値の null 参照 (Visual Basic では Nothing) に設定されます。

メモ    ImageList プロパティ値が null 参照 (Nothing) に変更された場合、 ImageIndex プロパティは既定値 -1 を返します。ただし、代入された ImageIndex 値は内部で保持され、この ImageList プロパティに別の ImageList オブジェクトが割り当てられたときに使用されます。 ImageList プロパティに割り当てられた新しい ImageListImageCollection.Count プロパティ値が、 ImageIndex プロパティに代入されている値から 1 を引いた値以下の場合 (コレクションが 0 から始まるインデックス番号であることを考慮)、 ImageIndex プロパティ値は Count プロパティ値より 1 小さい値に調整されます。たとえば、3 つのイメージがある ImageList を持ち、 ImageIndex プロパティが 2 に設定されているボタン コントロールがあるとします。このボタンに、イメージが 2 つしかない新しい ImageList を割り当てると、 ImageIndex 値は 1 に変更されます。

使用例

派生クラス Button を使用して、 ImageList プロパティと ImageIndex プロパティを設定する例を次に示します。このコードは、 ImageList が作成され、そこに Image が 1 つ以上割り当てられていることを前提にしています。また、 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

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ButtonBase クラス | ButtonBase メンバ | System.Windows.Forms 名前空間 | ImageList