Label.ImageIndex Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit la valeur d’index de l’image affichée sur le 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
Valeur de propriété
Index de base zéro qui représente la position dans le ImageList contrôle (affecté à la ImageList propriété) où se trouve l’image. La valeur par défaut est -1.
- Attributs
Exceptions
value est inférieur à -1.
Exemples
L’exemple de code suivant montre comment créer un Label contrôle qui a une bordure tridimensionnelle et une image affichée à l’aide des propriétés et ImageIndex des ImageList propriétés. Le contrôle a également une légende avec un caractère mnemonique spécifié. L’exemple de code utilise les propriétés et PreferredWidth les PreferredHeight propriétés pour dimensionner correctement le Label contrôle sur le formulaire sur lequel il est affiché. Cet exemple nécessite qu’une ImageList image a été créée et nommée imageList1 et qu’elle ait chargé deux images. L’exemple nécessite également que le code se trouve dans un formulaire dont l’espace System.Drawing de noms est ajouté à son code.
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
Remarques
Les ImageIndex propriétés et les ImageList propriétés ne peuvent pas être utilisées en même temps que la Image propriété. Lorsque la propriété et ImageList les ImageIndex propriétés sont utilisées pour afficher une image, la Image propriété est automatiquement définie nullsur .
ImageKey et ImageIndex s’excluent mutuellement, ce qui signifie que si l’un est défini, l’autre est défini sur une valeur non valide et ignoré. Si vous définissez la ImageKey propriété, la ImageIndex propriété est automatiquement définie sur -1. Sinon, si vous définissez la ImageIndex propriété, la ImageKey valeur est automatiquement définie sur une chaîne vide (« »).
Si la ImageList valeur de la propriété est modifiée null, la ImageIndex propriété retourne sa valeur par défaut, -1. Toutefois, la valeur affectée ImageIndex est conservée en interne et utilisée lorsqu’un autre ImageList objet est affecté à la ImageList propriété. Si la nouvelle ImageList propriété affectée à la ImageList propriété a une ImageList.ImageCollection.Count valeur de propriété inférieure ou égale à la valeur affectée à la ImageIndex propriété moins une (pour tenir compte de la collection étant un index de base zéro), la ImageIndex valeur de la propriété est ajustée à une valeur inférieure à celle de la Count propriété.
Par exemple, considérez un contrôle bouton dont trois ImageList images sont définies et dont ImageIndex la propriété est définie sur 2. Si une nouvelle ImageList image n’a que deux images sont affectées au bouton, la ImageIndex valeur passe à 1.