ListViewItem 類別

定義

表示 ListView 控制項中的項目。

public ref class ListViewItem : ICloneable, System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewItemConverter))]
[System.Serializable]
public class ListViewItem : ICloneable, System.Runtime.Serialization.ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewItemConverter))>]
[<System.Serializable>]
type ListViewItem = class
    interface ICloneable
    interface ISerializable
Public Class ListViewItem
Implements ICloneable, ISerializable
繼承
ListViewItem
屬性
實作

範例

下列程式碼範例會建立控制項,其中指定三 ListViewItem 個物件,以及針對每個專案指定的三 ListViewItem.ListViewSubItemListView 物件。 此範例也會建立 ColumnHeader 物件,以顯示詳細資料檢視中的子專案。 程式碼範例中也會建立兩 ImageList 個 物件,以提供 物件的影像 ListViewItem 。 這些 ImageList 物件會新增至 LargeImageListSmallImageList 屬性。 此範例會在建立 ListView 控制項時使用下列屬性:

您必須將程式碼新增至 Form ,並從表單上的建構函式或其他方法呼叫在範例中建立的方法。 此範例要求名為 MySmallImage1MySmallImage2MyLargeImage1 和 的 MyLargeImage2 映射位於磁片磁碟機 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

備註

控制項 ListView 類似于 ListBox ,因為它會顯示專案清單。 主要差異在於 ListView 控制項提供使用者可檢視專案的各種不同方式。 類別 ListViewItem 會定義與 控制項中顯示的 ListView 專案相關聯的外觀、行為和資料。 ListViewItem 物件可以在四個不同檢視的其中一個控制項中 ListView 顯示。 專案可以顯示為大型或小型圖示,或垂直清單中的小型圖示。 專案也可以有子專案,其中包含與父專案相關的資訊。 第四個檢視樣式詳細資料檢視可讓您在方格中顯示專案及其子專案,其中包含資料行標頭,可用來識別子專案中顯示的資訊。

類別的大部分屬性 ListViewItem 都提供變更其相關聯控制項中 ListView 專案顯示的方式。 BackColorForeColorFont 屬性可讓您變更專案文字在 控制項中的 ListView 顯示方式。 屬性 ImageIndex 可讓您藉由設定 LargeImageList) 的 或 SmallImageList 屬性,指定要從 ImageList 指派給 ListView 控制項 (載入的 ListView 影像。 專案可以顯示覆選框,以類似 CheckedListBox 控制項的方式,從使用者取得專案選擇。 您可以使用 Checked 屬性來判斷專案是否已核取,或在執行時間選取或清除核取方塊。 當相關聯 ListView 控制項的 屬性設定為 Details ,且控制項的 中 ListView.ColumnHeaderCollection 定義資料行時, View 專案可以顯示任意數目的 ListView 子專案。 您可以呼叫 Add 類別的 ListViewItem.ListViewSubItemCollection 方法,將子專案新增至專案。 屬性 SubItems 可讓您存取 ListViewItem.ListViewSubItemCollection 類別及其成員。

類別的某些 ListViewItem 屬性和方法是 控制項中 ListView 屬性和方法的專案特定版本。 例如,方法 EnsureVisible 類似于 ListView 方法的版本,但 ListViewItem 版本只會影響目前的專案。

類別 ListViewItem 也提供不是方法版本 ListView 的方法。 方法會將 BeginEdit 專案的文字放入編輯模式,讓使用者可以在控制項的 ListView 屬性設定為 true) 時 LabelEdit ,變更專案的文字 (。 方法 Clone 可讓您建立現有 ListViewItem 物件的複本,以便在其他 ListView 控制項中重複使用。

建構函式

ListViewItem()

使用預設值,初始化 ListViewItem 類別的新執行個體。

ListViewItem(ListViewGroup)

初始化 ListViewItem 類別的新執行個體,並將它指派給指定的群組。

ListViewItem(ListViewItem+ListViewSubItem[], Int32)

使用項目圖示的影像索引位置和 ListViewItem 物件的陣列,初始化 ListViewItem.ListViewSubItem 類別的新執行個體。

ListViewItem(ListViewItem+ListViewSubItem[], Int32, ListViewGroup)

使用項目圖示的影像索引位置和 ListViewItem 物件的陣列,初始化 ListViewItem.ListViewSubItem 類別的新執行個體,並將此項目指派給指定的群組。

ListViewItem(ListViewItem+ListViewSubItem[], String)

使用指定的子項目和影像,初始化 ListViewItem 類別的新執行個體。

ListViewItem(ListViewItem+ListViewSubItem[], String, ListViewGroup)

使用指定的子項目、影像和群組,初始化 ListViewItem 類別的新執行個體。

ListViewItem(SerializationInfo, StreamingContext)

以指定的序列化資訊和資料流內容,將 ListViewItem 類別的新執行個體初始化。

ListViewItem(String)

使用指定的項目文字,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String, Int32)

使用指定的項目文字和項目圖示的影像索引位置,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String, Int32, ListViewGroup)

使用指定的項目文字和項目圖示的影像索引位置,初始化 ListViewItem 類別的新執行個體,並將此項目指派給指定的群組。

ListViewItem(String, ListViewGroup)

使用指定的項目文字,初始化 ListViewItem 類別的新執行個體,並將它指派給指定的群組。

ListViewItem(String, String)

使用指定的文字和影像,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String, String, ListViewGroup)

使用指定的文字、影像和群組,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[])

使用代表子項目的字串陣列,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], Int32)

使用項目圖示的影像索引位置和表示子項目的字串陣列,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], Int32, Color, Color, Font)

使用項目圖示的影像索引位置、項目的前景色彩、背景色彩與字型以及表示子項目的字串陣列,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], Int32, Color, Color, Font, ListViewGroup)

使用項目圖示的影像索引位置、項目的前景色彩、背景色彩與字型以及表示子項目的字串陣列,初始化 ListViewItem 類別的新執行個體。 此外,將此項目指派給指定的群組。

ListViewItem(String[], Int32, ListViewGroup)

使用項目圖示的影像索引位置和表示子項目的字串陣列,初始化 ListViewItem 類別的新執行個體,並將此項目指派給指定的群組。

ListViewItem(String[], ListViewGroup)

使用表示子項目的字串陣列,初始化 ListViewItem 類別的新執行個體,並將此項目指派給指定的群組。

ListViewItem(String[], String)

使用指定的項目和子項目文字及影像,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], String, Color, Color, Font)

使用包含指定之文字、影像、色彩和字型的子項目,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], String, Color, Color, Font, ListViewGroup)

使用包含指定之文字、影像、色彩、字型和群組的子項目,初始化 ListViewItem 類別的新執行個體。

ListViewItem(String[], String, ListViewGroup)

使用包含指定之文字、影像和群組的子項目,初始化 ListViewItem 類別的新執行個體。

屬性

BackColor

取得或設定項目文字的背景色彩。

Bounds

取得項目、包括子項目的週框。

Checked

取得或設定值,指出項目是否已選取。

Focused

取得或設定值,指出 ListView 控制項中的項目是否具有焦點。

Font

取得或設定項目顯示之文字字型。

ForeColor

取得或設定項目文字的前景色彩。

Group

取得或設定指派此項目至其中的群組。

ImageIndex

取得或設定為項目顯示的影像索引。

ImageKey

取得或設定為項目顯示之影像的索引鍵。

ImageList

取得 ImageList,包含隨項目顯示的影像。

IndentCount

取得或設定用以縮排 ListViewItem 之小型影像寬度的數目。

Index

取得項目在 ListView 控制項中之以零起始的索引。

ListView

取得包含項目的 ListView 控制項。

Name

取得或設定與這個 ListViewItem 相關聯的名稱。

Position

取得或設定 ListViewItem 左上角的位置。

Selected

取得或設定值,表示是否選取項目。

StateImageIndex

取得或設定為項目顯示之狀態影像的索引 (例如已選取或已清除核取方塊的影像,表示項目的狀態)。

SubItems

取得集合,其中包含項目所有的子項目。

Tag

取得或設定物件,其中包含與項目相關的資料。

Text

取得或設定項目文字。

ToolTipText

取得或設定當滑鼠指標停留在 ListViewItem 上時所顯示的文字。

UseItemStyleForSubItems

取得或設定值,指出項目的 FontForeColorBackColor 屬性是否要用在項目所有的子項目中。

方法

BeginEdit()

將項目文字放入編輯模式。

Clone()

建立相同的項目副本。

Deserialize(SerializationInfo, StreamingContext)

可將項目還原序列化。

EnsureVisible()

可確定項目在控制項中為可見,必要時捲動控制項的內容。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FindNearestItem(SearchDirectionHint)

ListViewItem 中尋找下一個項目 (朝指定的方向搜尋)。

GetBounds(ItemBoundsPortion)

可擷取項目週框的指定部分。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetSubItemAt(Int32, Int32)

傳回位於指定座標之 ListViewItem 的子項目。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove()

將項目從關聯的 ListView 控制項中移除。

Serialize(SerializationInfo, StreamingContext)

序列化項目。

ToString()

傳回代表目前物件的字串。

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

序列化項目。

適用於

另請參閱