Label.ImageIndex Vlastnost

Definice

Získá nebo nastaví index hodnotu obrázku zobrazeného na .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

Hodnota vlastnosti

Int32

Index založený na nule, který představuje pozici v ovládacím ImageList prvku (přiřazené ImageList vlastnosti), kde se nachází obrázek. Výchozí hodnota je -1.

Atributy

Výjimky

value je menší než -1.

Příklady

Následující příklad kódu ukazuje, jak vytvořit Label ovládací prvek, který má trojrozměrné ohraničení a obrázek zobrazený pomocí ImageList a ImageIndex vlastností. Ovládací prvek má také titulek se zadaným měmónickým znakem. Ukázkový kód používá PreferredHeight vlastnosti k PreferredWidth správné velikosti Label ovládacího prvku ve formuláři, na kterém se zobrazí. Tento příklad vyžaduje vytvoření ImageList a pojmenování imageList1 a načtení dvou imagí. Tento příklad také vyžaduje, aby kód byl ve formuláři s přidaným oborem System.Drawing názvů do kódu.

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

Poznámky

Vlastnosti ImageIndex a vlastnosti ImageList nelze použít současně s Image vlastností. ImageIndex Pokud se vlastnost a ImageList vlastnosti používají k zobrazení obrázku, Image vlastnost je automaticky nastavena na null.

ImageKey a ImageIndex vzájemně se vylučují, což znamená, že pokud je nastavena jedna, druhá je nastavena na neplatnou hodnotu a ignorována. Pokud vlastnost nastavíte ImageKey , ImageIndex vlastnost se automaticky nastaví na -1. Případně pokud vlastnost nastavíte ImageIndex , nastaví se ImageKey automaticky na prázdný řetězec ("").

ImageList Pokud se hodnota vlastnosti změní na null, ImageIndex vrátí vlastnost výchozí hodnotu -1. Přiřazená ImageIndex hodnota se ale uchovává interně a používá se při přiřazení jiného ImageList objektu k ImageList vlastnosti. Pokud má nová přiřazená ImageList vlastnost ImageList.ImageCollection.Count hodnotu vlastnosti, která je menší nebo rovna hodnotě přiřazené k ImageIndex vlastnosti minus jedna (pro účet kolekce je nulový index), ImageIndex hodnota vlastnosti se upraví na jednu menší než Count hodnota ImageList vlastnosti. Představte si například ovládací prvek tlačítka, jehož ImageList má tři obrázky a jehož ImageIndex vlastnost je nastavená na hodnotu 2. Pokud je tlačítku přiřazena nová ImageList , která má pouze dva obrázky, změní se ImageIndex hodnota na 1.

Platí pro

Viz také