Label.ImageList 属性

定义

获取或设置包含要在 ImageList 控件中显示的图像的 LabelGets or sets the ImageList that contains the images to display in the Label control.

public:
 property System::Windows::Forms::ImageList ^ ImageList { System::Windows::Forms::ImageList ^ get(); void set(System::Windows::Forms::ImageList ^ value); };
public System.Windows.Forms.ImageList ImageList { get; set; }
member this.ImageList : System.Windows.Forms.ImageList with get, set
Public Property ImageList As ImageList

属性值

ImageList,它存储 Image 对象集合。An ImageList that stores the collection of Image objects. 默认值为 nullThe default value is null.

示例

下面的代码示例演示如何创建一个 Label 控件,该控件具有三维边框和使用 ImageListImageIndex 属性显示的图像。The following code example demonstrates how to create a Label control that has a three-dimensional border and an image displayed using the ImageList and ImageIndex properties. 该控件还具有一个标题,其中指定了助记符。The control also has a caption with a mnemonic character specified. 示例代码使用 PreferredHeightPreferredWidth 属性在显示该控件的窗体上正确调整 Label 控件的大小。The example code uses the PreferredHeight and PreferredWidth properties to properly size the Label control on the form on which it is displayed. 此示例要求已创建了 ImageList 并将其命名为 imageList1,并且已加载了两个图像。This example requires that an ImageList has been created and named imageList1 and that it has loaded two images. 该示例还要求代码位于将 System.Drawing 命名空间添加到其代码的窗体中。The example also requires that the code is within a form that has the System.Drawing namespace added to its 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

注解

ImageIndexImageList 属性不能与 Image 属性同时使用。The ImageIndex and the ImageList properties cannot be used at the same time as the Image property. ImageIndex 属性和 ImageList 属性用于显示图像时,Image 属性设置为 nullWhen the ImageIndex property and ImageList properties are used to display an image, the Image property is set to null.

备注

如果在其他位置仍引用 Label,则不会对 ImageList 其进行垃圾回收。The Label will not be garbage collected if its ImageList is still referenced elsewhere. LabelImageList 属性设置为 null,以启用高效的垃圾收集。Set the ImageList property of the Label to null to enable efficient garbage collection.

适用于

另请参阅