Поделиться через


Label.ImageIndex Свойство

Определение

Возвращает или задает значение индекса изображения, отображаемого на экране 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

Значение свойства

Отсчитываемый от нуля индекс, представляющий позицию в ImageList элементе управления (назначено ImageList свойству), где находится изображение. Значение по умолчанию — -1.

Атрибуты

Исключения

value меньше -1.

Примеры

В следующем примере кода показано, как создать Label элемент управления с трехмерной границей и изображением, отображаемым с помощью ImageList и ImageIndex свойства. Элемент управления также содержит заголовок с заданным символом mnemonic. В примере кода используются PreferredHeight свойства и PreferredWidth свойства для правильного Label размера элемента управления в форме, на которой она отображается. В этом примере требуется, чтобы ImageList созданный и именованный imageList1 был создан и загружен два образа. В примере также требуется, чтобы код находится в форме с пространством имен, System.Drawing добавленным в его код.

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

Комментарии

ImageList Свойства ImageIndex нельзя использовать одновременно с свойствомImage. ImageIndex Если свойство и ImageList свойства используются для отображения изображения, Image свойство автоматически задаетсяnull.

ImageKey и ImageIndex являются взаимоисключающими, то есть, если задано одно, другое имеет недопустимое значение и игнорируется. Если задано ImageKey свойство, ImageIndex свойство автоматически присваивается значение -1. Кроме того, если задать ImageIndex свойство, ImageKey оно автоматически присваивается пустой строке ("").

ImageList Если значение свойства измененоnull, ImageIndex свойство возвращает значение по умолчанию -1. Однако назначенное ImageIndex значение сохраняется внутри и используется при назначении другого ImageList объекта свойству ImageList . Если новый ImageList назначенный ImageList свойству имеет ImageList.ImageCollection.Count значение свойства, которое меньше или равно значению, назначенному ImageIndex свойству минус один (для учета коллекции, являющейся отсчитываемой от нуля индексом), ImageIndex значение свойства корректируется до одного меньше Count значения свойства. Например, рассмотрим элемент управления кнопкой с ImageList тремя изображениями и свойством которого ImageIndex задано значение 2. Если для кнопки назначено только ImageList два изображения, ImageIndex значение изменится на 1.

Применяется к

См. также раздел