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

屬性值

Int32

以零起始的索引,表示在 ImageList 控制項中 (指派至 ImageList 屬性) 影像所放置的位置。 預設值為 -1。

屬性

例外狀況

value 小於 -1。

範例

下列程式碼範例示範如何建立 Label 控制項,其中包含使用 和 ImageIndex 屬性顯示的 ImageList 三維框線和影像。 控制項也有指定助記字元的標題。 範例程式碼會使用 PreferredHeightPreferredWidth 屬性,在顯示控制項的表單上適當地調整其大小 Label 。 此範例要求 ImageList 已建立並命名為 imageList1,且已載入兩個映射。 此範例也需要程式碼位於命名空間已新增至其程式碼的表單 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

備註

ImageIndexImageList 屬性不能與 屬性同時 Image 使用。 ImageIndex當屬性和 ImageList 屬性用來顯示影像時, Image 屬性會自動設定為 null

ImageKeyImageIndex 互斥,這表示如果已設定,另一個設定為不正確值並忽略。 如果您設定 ImageKey 屬性, ImageIndex 則屬性會自動設定為 -1。 或者,如果您設定 ImageIndex 屬性,會自動 ImageKey 將 設定為空字串 (「」「) 。

ImageList如果屬性值變更為 null ,則 ImageIndex 屬性會傳回其預設值 -1。 不過,指派 ImageIndex 的值會在內部保留,並在將另一個 ImageList 物件指派給 ImageList 屬性時使用。 如果指派給 ImageList 屬性的新 ImageList 具有 ImageList.ImageCollection.Count 小於或等於指派給 ImageIndex 屬性的值減一 (,以將集合視為以零起始的索引) ImageIndex ,屬性值會調整為小於 Count 屬性值的一個。 例如,假設按鈕控制項有 ImageList 三個影像,且其 ImageIndex 屬性設定為 2。 如果只有兩個影像的新 ImageList 指派給按鈕,此值 ImageIndex 會變更為 1。

適用於

另請參閱