Label.ImageIndex プロパティ

定義

Label に表示されているイメージのインデックス値を取得または設定します。

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

プロパティ値

イメージが配置されている ImageList コントロール内の位置を表す、0 から始まるインデックス値 (ImageList プロパティに代入される)。 既定値は -1 です。

属性

例外

value が -1 未満です。

次のコード例では、 プロパティと ImageIndex プロパティをLabel使用して表示される 3 次元罫線と画像を含むコントロールを作成する方法をImageList示します。 コントロールには、ニーモニック文字が指定されたキャプションもあります。 コード例では、 プロパティと PreferredWidth プロパティをPreferredHeight使用して、Label表示されるフォームのコントロールのサイズを適切に設定します。 この例では、 が ImageList 作成され、imageList1 という名前が付けられ、2 つのイメージが読み込まれている必要があります。 この例では、コードが、コードに名前空間が追加された System.Drawing フォーム内にある必要もあります。

public:
   void CreateMyLabel()
   {
      // Create an instance of a Label.
      Label^ label1 = gcnew Label;
      
      // Set the border to a three-dimensional border.
      label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
      // Set the ImageList to use for displaying an image.
      label1->ImageList = imageList1;
      // Use the second image in imageList1.
      label1->ImageIndex = 1;
      // Align the image to the top left corner.
      label1->ImageAlign = ContentAlignment::TopLeft;
      
      // Specify that the text can display mnemonic characters.
      label1->UseMnemonic = true;
      // Set the text of the control and specify a mnemonic character.
      label1->Text = "First &Name:";
      
      /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
      label1->Size = System::Drawing::Size( label1->PreferredWidth, label1->PreferredHeight );
      
      //...Code to add the control to the form...
   }
public void CreateMyLabel()
{
   // Create an instance of a Label.
   Label label1 = new Label();

   // Set the border to a three-dimensional border.
   label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   // Set the ImageList to use for displaying an image.
   label1.ImageList = imageList1;
   // Use the second image in imageList1.
   label1.ImageIndex = 1;
   // Align the image to the top left corner.
   label1.ImageAlign = ContentAlignment.TopLeft;

   // Specify that the text can display mnemonic characters.
   label1.UseMnemonic = true;
   // Set the text of the control and specify a mnemonic character.
   label1.Text = "First &Name:";
   
   /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
   label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);

   //...Code to add the control to the form...
}
Public Sub CreateMyLabel()
    ' Create an instance of a Label.
    Dim label1 As New Label()
       
    ' Set the border to a three-dimensional border.
    label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    ' Set the ImageList to use for displaying an image.
    label1.ImageList = imageList1
    ' Use the second image in imageList1.
    label1.ImageIndex = 1
    ' Align the image to the top left corner.
    label1.ImageAlign = ContentAlignment.TopLeft
     
    ' Specify that the text can display mnemonic characters.
    label1.UseMnemonic = True
    ' Set the text of the control and specify a mnemonic character.
    label1.Text = "First &Name:"
       
    ' Set the size of the control based on the PreferredHeight and PreferredWidth values. 
    label1.Size = New Size(label1.PreferredWidth, label1.PreferredHeight)

    '...Code to add the control to the form...
End Sub

注釈

ImageIndexプロパティと プロパティはImageList、 プロパティと同時にImage使用することはできません。 プロパティとプロパティをImageIndex使用してイメージを表示すると、 Image プロパティは自動的に にnull設定ImageListされます。

ImageKeyImageIndex は相互に排他的です。つまり、一方が設定されている場合、もう一方は無効な値に設定され、無視されます。 プロパティを ImageKey 設定すると、 ImageIndex プロパティは自動的に -1 に設定されます。 または、 プロパティを ImageIndex 設定すると、 ImageKey は自動的に空の文字列 ("") に設定されます。

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

適用対象

こちらもご覧ください