Label.ImageIndex Właściwość

Definicja

Pobiera lub ustawia wartość indeksu obrazu wyświetlanego 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

Wartość właściwości

Int32

Indeks oparty na zerze, który reprezentuje pozycję w kontrolce ImageList (przypisanej ImageList do właściwości), w której znajduje się obraz. Wartość domyślna to -1.

Atrybuty

Wyjątki

value wartość jest mniejsza niż -1.

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć kontrolkę Label z obramowaniem trójwymiarowym i obrazem wyświetlanym przy użyciu ImageList właściwości i ImageIndex . Kontrolka ma również podpis z określonym znakiem mnemonic. Przykładowy kod używa PreferredHeight właściwości i PreferredWidth do prawidłowego rozmiaru Label kontrolki w formularzu, na którym jest wyświetlany. Ten przykład wymaga ImageList utworzenia i nazwy imageList1 oraz załadowania dwóch obrazów. Przykład wymaga również, aby kod mieścił się w formularzu, który ma System.Drawing przestrzeń nazw dodaną do jego kodu.

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

Uwagi

Właściwości ImageIndex i ImageList nie mogą być używane w tym samym czasie co Image właściwość . ImageIndex Gdy właściwość i ImageList właściwości są używane do wyświetlania obrazu, Image właściwość jest automatycznie ustawiana na nullwartość .

ImageKey i ImageIndex wzajemnie wykluczają się, co oznacza, że jeśli jeden jest ustawiony, drugi jest ustawiony na nieprawidłową wartość i ignorowany. Jeśli ustawisz ImageKey właściwość , właściwość zostanie automatycznie ustawiona ImageIndex na -1. Alternatywnie, jeśli ustawisz ImageIndex właściwość , ImageKey właściwość zostanie automatycznie ustawiona na pusty ciąg ("").

Jeśli wartość właściwości zostanie zmieniona ImageList na null, ImageIndex właściwość zwróci jej wartość domyślną, -1. Jednak przypisana ImageIndex wartość jest zachowywana wewnętrznie i używana, gdy do właściwości jest przypisany ImageList inny ImageList obiekt. Jeśli nowa ImageList przypisana do ImageList właściwości ma wartość właściwości mniejszą lub równą ImageList.ImageCollection.Count wartości przypisanej ImageIndex do właściwości minus jeden (do konta kolekcji będącej indeksem opartym na zerze), ImageIndex wartość właściwości jest dostosowywana do jednej mniejszej niż Count wartość właściwości. Rozważmy na przykład kontrolkę ImageList przycisku z trzema obrazami i właściwość ImageIndex ustawioną na 2. Jeśli nowy ImageList , który ma tylko dwa obrazy, zostanie przypisany do przycisku, ImageIndex wartość zmieni się na 1.

Dotyczy

Zobacz też