Label.ImageIndex Propiedad

Definición

Obtiene o establece el valor de índice de la imagen que se muestra en 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 de propiedad

Índice basado en cero que representa la posición donde está ubicada la imagen en el control ImageList (asignado a la propiedad ImageList). El valor predeterminado es -1.

Atributos

Excepciones

value es menor que 1.

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un Label control que tenga un borde tridimensional y una imagen mostrada mediante las ImageList propiedades y ImageIndex . El control también tiene un título con un carácter mnemónico especificado. El código de ejemplo usa las PreferredHeight propiedades y PreferredWidth para ajustar correctamente el tamaño del Label control en el formulario en el que se muestra. En este ejemplo se requiere que ImageList se haya creado y denominado imageList1 y que haya cargado dos imágenes. El ejemplo también requiere que el código esté dentro de un formulario que tenga el System.Drawing espacio de nombres agregado a su 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

Comentarios

Las ImageIndex propiedades y ImageList no se pueden usar al mismo tiempo que la Image propiedad . Cuando la propiedad y ImageList las ImageIndex propiedades se usan para mostrar una imagen, la Image propiedad se establece automáticamente en null.

ImageKey y ImageIndex son mutuamente excluyentes, lo que significa que si se establece uno, el otro se establece en un valor no válido y se omite. Si establece la ImageKey propiedad , la ImageIndex propiedad se establece automáticamente en -1. Como alternativa, si establece la ImageIndex propiedad , se ImageKey establece automáticamente en una cadena vacía ("").

Si el valor de la ImageList propiedad se cambia a null, la ImageIndex propiedad devuelve su valor predeterminado, -1. Sin embargo, el valor asignado ImageIndex se conserva internamente y se usa cuando se asigna otro ImageList objeto a la ImageList propiedad . Si el nuevo ImageList asignado a la ImageList propiedad tiene un ImageList.ImageCollection.Count valor de propiedad menor o igual que el valor asignado a la ImageIndex propiedad menos uno (para tener en cuenta que la colección es un índice de base cero), el ImageIndex valor de la propiedad se ajusta a uno menor que el valor de la Count propiedad. Por ejemplo, considere un control de botón cuya ImageList propiedad tiene tres imágenes y cuya ImageIndex propiedad está establecida en 2. Si un nuevo ImageList que solo tiene dos imágenes se asigna al botón, el ImageIndex valor cambia a 1.

Se aplica a

Consulte también