Bagikan melalui


Label.ImageIndex Properti

Definisi

Mendapatkan atau mengatur nilai indeks gambar yang ditampilkan pada 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

Nilai Properti

Indeks berbasis nol yang mewakili posisi dalam ImageList kontrol (ditetapkan ke ImageList properti) tempat gambar berada. Defaultnya adalah -1.

Atribut

Pengecualian

value kurang dari -1.

Contoh

Contoh kode berikut menunjukkan cara membuat Label kontrol yang memiliki batas tiga dimensi dan gambar yang ditampilkan menggunakan ImageList properti dan ImageIndex . Kontrol juga memiliki keterangan dengan karakter mnemonic yang ditentukan. Contoh kode menggunakan PreferredHeight properti dan PreferredWidth untuk mengukur Label kontrol dengan benar pada formulir tempat kode ditampilkan. Contoh ini mengharuskan telah ImageList dibuat dan diberi nama imageList1 dan telah memuat dua gambar. Contoh ini juga mengharuskan kode berada dalam formulir yang memiliki System.Drawing namespace layanan yang ditambahkan ke kodenya.

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

Keterangan

Properti ImageIndex dan ImageList tidak dapat digunakan pada saat yang sama dengan Image properti . ImageIndex Ketika properti dan ImageList properti digunakan untuk menampilkan gambar, Image properti secara otomatis diatur ke null.

ImageKey dan ImageIndex saling eksklusif, yang berarti jika satu diatur, yang lain diatur ke nilai yang tidak valid dan diabaikan. Jika Anda mengatur ImageKey properti , ImageIndex properti secara otomatis diatur ke -1. Atau, jika Anda mengatur ImageIndex properti , ImageKey secara otomatis diatur ke string kosong ("").

ImageList Jika nilai properti diubah menjadi null, ImageIndex properti mengembalikan nilai defaultnya, -1. Namun, nilai yang ditetapkan ImageIndex dipertahankan secara internal dan digunakan ketika objek lain ImageList ditetapkan ke ImageList properti . Jika yang baru ImageList ditetapkan ke ImageList properti memiliki ImageList.ImageCollection.Count nilai properti yang kurang dari atau sama dengan nilai yang ditetapkan ke ImageIndex properti dikurangi satu (untuk memperhitungkan koleksi menjadi indeks berbasis nol), ImageIndex nilai properti disesuaikan menjadi satu kurang dari Count nilai properti. Misalnya, pertimbangkan kontrol tombol yang ImageList memiliki tiga gambar dan yang propertinya ImageIndex diatur ke 2. Jika baru ImageList yang hanya memiliki dua gambar ditetapkan ke tombol , nilai berubah ImageIndex menjadi 1.

Berlaku untuk

Lihat juga