Share via


Label.ImageIndex Propriedade

Definição

Obtém ou define o valor de índice da imagem exibida no 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

Valor da propriedade

Int32

Um índice baseado em zero que representa a posição no controle ImageList (atribuído à propriedade ImageList) em que a imagem está localizada. O padrão é -1.

Atributos

Exceções

value é menor que -1.

Exemplos

O exemplo de código a seguir demonstra como criar um Label controle que tenha uma borda tridimensional e uma imagem exibida usando as propriedades eImageIndex.ImageList O controle também tem uma legenda com um caractere mnemônico especificado. O código de exemplo usa as PreferredHeight propriedades e PreferredWidth para dimensionar corretamente o Label controle no formulário no qual ele é exibido. Este exemplo requer que um ImageList imageList1 foi criado e nomeado e que tenha carregado duas imagens. O exemplo também exige que o código esteja dentro de um formulário que tenha o System.Drawing namespace adicionado ao código.

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

Comentários

As ImageIndex propriedades e as ImageList propriedades não podem ser usadas ao mesmo tempo que a Image propriedade. Quando a propriedade e ImageList as ImageIndex propriedades são usadas para exibir uma imagem, a Image propriedade é definida automaticamente como null.

ImageKey e ImageIndex são mutuamente exclusivos, ou seja, se um for definido, o outro será definido como um valor inválido e ignorado. Se você definir a ImageKey propriedade, a ImageIndex propriedade será definida automaticamente como -1. Como alternativa, se você definir a ImageIndex propriedade, ela ImageKey será definida automaticamente como uma cadeia de caracteres vazia ("").

Se o valor da ImageList propriedade for alterado, nulla ImageIndex propriedade retornará seu valor padrão, -1. No entanto, o valor atribuído ImageIndex é retido internamente e usado quando outro ImageList objeto é atribuído à ImageList propriedade. Se o novo ImageList atribuído à ImageList propriedade tiver um ImageList.ImageCollection.Count valor de propriedade menor ou igual ao valor atribuído à ImageIndex propriedade menos um (para considerar que a coleção é um índice baseado em zero), o valor da ImageIndex propriedade será ajustado para um menor que o valor da Count propriedade. Por exemplo, considere um controle de botão cujas ImageList imagens têm três imagens e cuja ImageIndex propriedade é definida como 2. Se uma nova ImageList que tem apenas duas imagens for atribuída ao botão, o ImageIndex valor será alterado para 1.

Aplica-se a

Confira também