Share via


ListView.View Properti

Definisi

Mendapatkan atau mengatur bagaimana item ditampilkan dalam kontrol.

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

Nilai Properti

Salah View satu nilai. Default adalah LargeIcon.

Pengecualian

Nilai yang ditentukan bukan salah View satu nilai.

Contoh

Contoh kode berikut membuat ListView kontrol dengan tiga ListViewItem objek yang ditentukan dan tiga ListViewItem.ListViewSubItem objek yang ditentukan untuk setiap item. Contohnya juga membuat ColumnHeader objek untuk menampilkan subitem dalam tampilan detail. Dua ImageList objek juga dibuat dalam contoh kode untuk menyediakan gambar untuk ListViewItem objek. Objek ini ImageList ditambahkan ke LargeImageList properti dan SmallImageList . Contohnya menggunakan properti berikut dalam membuat ListView kontrol.

Contoh ini mengharuskan Anda telah menambahkan kode ke dan memanggil metode yang Form dibuat dalam contoh dari konstruktor atau metode lain pada formulir. Contohnya juga mengharuskan gambar bernama MySmallImage1, MySmallImage2, MyLargeImage1, dan MyLargeImage2 terletak di direktori akar drive C.

private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      // Width of -2 indicates auto-size.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    ' Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub

Keterangan

Properti View memungkinkan Anda menentukan tipe tampilan yang ListView digunakan kontrol untuk menampilkan item. Anda dapat mengatur View properti untuk menampilkan setiap item dengan ikon besar atau kecil atau menampilkan item dalam daftar vertikal. Opsi terkaya adalah tampilan detail, yang memungkinkan Anda untuk melihat tidak hanya item tetapi subitem apa pun yang ditentukan untuk setiap item. Setiap item ditampilkan dalam kisi, dengan setiap item tercantum secara vertikal dan subitem untuk setiap item ditampilkan dalam kolom, dengan header kolom. Tampilan detail adalah cara sempurna untuk menampilkan informasi database kepada pengguna. Dengan Windows XP dan Windows Server 2003, Anda juga dapat menampilkan item sebagai petak peta yang menyeimbangkan informasi grafis dan tekstual dengan memperlihatkan ikon besar bersama dengan informasi subitem yang Anda pilih. Untuk mengaktifkan tampilan petak peta, aplikasi Anda harus memanggil Application.EnableVisualStyles metode . Tampilan gambar kecil menampilkan setiap item dengan ikon dan informasi tekstual di sebelah kanan ikon. Tampilan gambar besar menampilkan setiap item dengan ikon dan informasi tekstual di bawah ikon. Ukuran ikon untuk daftar gambar ditentukan oleh ImageSize properti ImageList untuk SmallImageList properti atau LargeImageList .

Catatan

Jika Anda menggunakan beberapa daftar gambar, untuk tampilan ikon kecil dan besar, dengan ListView kontrol, Anda harus menempatkan versi gambar yang kecil dan besar di lokasi indeks yang sama di daftar gambar masing-masing. Saat beralih antar tampilan, lokasi indeks gambar dalam satu daftar digunakan untuk menemukan gambar di daftar lain, terlepas dari nilai kunci yang ditentukan.

Sebagian besar properti dalam ListView kontrol memengaruhi perilaku tampilan yang berbeda atau ditampilkan. Beberapa properti yang memengaruhi tampilan item hanya berguna ketika View properti diatur ke nilai tertentu, sementara properti lain berguna di semua tampilan. Misalnya, properti seperti GridLines dan FullRowSelect hanya berguna ketika View properti diatur ke View.Details, sementara MultiSelect properti dan CheckBoxes berguna di semua tampilan.

Tabel berikut ini memperlihatkan beberapa ListView anggota dan tampilan tempat mereka valid.

Anggota ListView Tampilan
Properti Alignment. SmallIcon atau LargeIcon
Properti AutoArrange. SmallIcon atau LargeIcon
metode AutoResizeColumn Details
CheckBoxes Semua tampilan kecuali Tile
Properti Columns. Details atau Tile
Peristiwa DrawSubItem Details
metode FindItemWithText Details, List, atau Tile
metode FindNearestItem SmallIcon atau LargeIcon
metode GetItemAt Details atau Tile
Properti Groups. Semua tampilan kecuali List
Properti HeaderStyle. Details
Properti InsertionMark. LargeIcon, SmallIcon, atau Tile

Anda dapat menggunakan View properti untuk memberikan tampilan data yang berbeda dalam aplikasi Anda, atau untuk mengunci tampilan tertentu untuk memanfaatkan manfaat tampilan tersebut. Misalnya, View properti sering diatur ke View.Details karena tampilan detail menyediakan sejumlah opsi tampilan yang tidak tersedia di tampilan lain.

Catatan

Jika kontrol Anda ListView tidak memiliki header kolom yang ditentukan dan Anda mengatur View properti ke View.Details, ListView kontrol tidak akan menampilkan item apa pun. Jika kontrol Anda ListView tidak memiliki header kolom yang ditentukan dan Anda mengatur View properti ke View.Tile, ListView kontrol tidak akan menampilkan subitem apa pun.

Tampilan petak menampilkan setiap item dengan ikon besar di sebelah kiri dan informasi tekstual di sebelah kanan. Informasi tekstual terdiri dari label item diikuti oleh subitem. Secara default, hanya subitem pertama yang ditampilkan, yang sesuai dengan label item. Untuk menampilkan subitem tambahan, Anda harus menambahkan ColumnHeader objek ke Columns koleksi. Setiap subitem dalam petak peta sesuai dengan header kolom. Untuk mengontrol subitem mana yang ditampilkan dan urutan tampilannya, Anda harus mengatur ListViewItem.ListViewSubItem.Name properti untuk setiap item dan ColumnHeader.Name properti untuk setiap header. Anda kemudian dapat menambahkan, menghapus, dan menyusun ulang header dalam Columns koleksi untuk mencapai hasil yang diinginkan.

Untuk mengontrol ukuran petak peta dalam tampilan petak peta, atur TileSize properti . Ini berguna untuk mencegah pembungkusan baris ketika teks subitem terlalu panjang untuk satu baris.

Untuk contoh tampilan petak peta, lihat TileSize properti .

Catatan

Meskipun kolom hanya ditampilkan dalam tampilan detail, subitem tanpa header kolom tidak akan ditampilkan dalam tampilan detail atau tampilan petak peta.

Tampilan petak peta hanya tersedia di Windows XP dan Windows Server 2003 saat aplikasi Anda memanggil Application.EnableVisualStyles metode . Pada sistem operasi sebelumnya, kode apa pun yang terkait dengan tampilan petak peta tidak berpengaruh, dan ListView kontrol ditampilkan dalam tampilan ikon besar. Akibatnya, kode apa pun yang bergantung pada tampilan petak mungkin tidak berfungsi dengan benar.

Anda mungkin ingin menyertakan kode yang menentukan apakah tampilan petak peta tersedia, dan menyediakan fungsionalitas alternatif saat tidak tersedia. Misalnya, saat Anda menggunakan gambar pemilik untuk menyesuaikan tampilan ListView item dalam tampilan petak peta, Anda mungkin ingin menggunakan kode gambar yang sesuai dengan tampilan ikon besar saat berjalan pada sistem operasi yang tidak mendukung tampilan petak peta.

Fitur tampilan petak peta disediakan oleh pustaka yang sama yang menyediakan fitur tema sistem operasi. Untuk memeriksa ketersediaan pustaka ini, panggil FeatureSupport.IsPresent(Object) metode kelebihan beban dan teruskan nilainya OSFeature.Themes .

Berlaku untuk

Lihat juga