ListView.ListViewItemCollection 類別

定義

代表 ListView 控制項中項目的集合,或指派給 ListViewGroup 之項目的集合。Represents the collection of items in a ListView control or assigned to a ListViewGroup.

public: ref class ListView::ListViewItemCollection : System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public class ListView.ListViewItemCollection : System.Collections.IList
type ListView.ListViewItemCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListView.ListViewItemCollection
Implements IList
繼承
ListView.ListViewItemCollection
屬性
實作

範例

下列程式碼範例示範如何將專案加入至ListView.ListViewItemCollectionThe following code example demonstrates how to add items to a ListView.ListViewItemCollection. 請注意, ItemsListView1屬性屬於型ListView.ListViewItemCollection別。Note that the Items property of ListView1 is of type ListView.ListViewItemCollection. 若要執行此範例,請將下列程式ListView代碼貼入包含名為ListView1之物件的表單TextBox1中,以及名為的TextBoxTo run this example, paste the following code into a form that contains a ListView object named ListView1 and a TextBox named TextBox1. 從表單的「函式」或Load 「事件處理常式」呼叫方法。InitializeListViewCall the InitializeListView method from the form's constructor or Load event handler.

// This method adds two columns to the ListView, setting the Text 
// and TextAlign, and Width properties of each ColumnHeader.  The 
// HeaderStyle property is set to NonClickable since the ColumnClick 
// event is not handled.  Finally the method adds ListViewItems and 
// SubItems to each column.
void InitializeListView()
{
   this->ListView1 = gcnew System::Windows::Forms::ListView;
   this->ListView1->BackColor = System::Drawing::SystemColors::Control;
   this->ListView1->Dock = System::Windows::Forms::DockStyle::Top;
   this->ListView1->Location = System::Drawing::Point( 0, 0 );
   this->ListView1->Name = "ListView1";
   this->ListView1->Size = System::Drawing::Size( 292, 130 );
   this->ListView1->TabIndex = 0;
   this->ListView1->View = System::Windows::Forms::View::Details;
   this->ListView1->MultiSelect = true;
   this->ListView1->HideSelection = false;
   this->ListView1->HeaderStyle = ColumnHeaderStyle::Nonclickable;
   ColumnHeader^ columnHeader1 = gcnew ColumnHeader;
   columnHeader1->Text = "Breakfast Item";
   columnHeader1->TextAlign = HorizontalAlignment::Left;
   columnHeader1->Width = 146;
   ColumnHeader^ columnHeader2 = gcnew ColumnHeader;
   columnHeader2->Text = "Price Each";
   columnHeader2->TextAlign = HorizontalAlignment::Center;
   columnHeader2->Width = 142;
   this->ListView1->Columns->Add( columnHeader1 );
   this->ListView1->Columns->Add( columnHeader2 );
   array<String^>^foodList = {"Juice","Coffee","Cereal & Milk","Fruit Plate","Toast & Jelly","Bagel & Cream Cheese"};
   array<String^>^foodPrice = {"1.09","1.09","2.19","2.49","1.49","1.49"};
   for ( int count = 0; count < foodList->Length; count++ )
   {
      ListViewItem^ listItem = gcnew ListViewItem( foodList[ count ] );
      listItem->SubItems->Add( foodPrice[ count ] );
      ListView1->Items->Add( listItem );

   }
   this->Controls->Add( ListView1 );
}
  // This method adds two columns to the ListView, setting the Text 
  // and TextAlign, and Width properties of each ColumnHeader.  The 
  // HeaderStyle property is set to NonClickable since the ColumnClick 
  // event is not handled.  Finally the method adds ListViewItems and 
  // SubItems to each column.
  private void InitializeListView()
  {
      this.ListView1 = new System.Windows.Forms.ListView();
      this.ListView1.BackColor = System.Drawing.SystemColors.Control;
      this.ListView1.Dock = System.Windows.Forms.DockStyle.Top;
      this.ListView1.Location = new System.Drawing.Point(0, 0);
      this.ListView1.Name = "ListView1";
      this.ListView1.Size = new System.Drawing.Size(292, 130);
      this.ListView1.TabIndex = 0;
      this.ListView1.View = System.Windows.Forms.View.Details;
      this.ListView1.MultiSelect = true;
      this.ListView1.HideSelection = false;
      this.ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
      
      ColumnHeader columnHeader1 = new ColumnHeader();
      columnHeader1.Text = "Breakfast Item";
      columnHeader1.TextAlign = HorizontalAlignment.Left;
      columnHeader1.Width = 146;

      ColumnHeader columnHeader2 = new ColumnHeader();
      columnHeader2.Text = "Price Each";
      columnHeader2.TextAlign = HorizontalAlignment.Center;
      columnHeader2.Width = 142;

      this.ListView1.Columns.Add(columnHeader1);
      this.ListView1.Columns.Add(columnHeader2);

      string[] foodList = new string[]{"Juice", "Coffee", 
          "Cereal & Milk", "Fruit Plate", "Toast & Jelly", 
          "Bagel & Cream Cheese"};
      string[] foodPrice = new string[]{"1.09", "1.09", "2.19", 
          "2.49", "1.49", "1.49"};
      
      for(int count=0; count < foodList.Length; count++)
      {
          ListViewItem listItem = new ListViewItem(foodList[count]);
          listItem.SubItems.Add(foodPrice[count]);
          ListView1.Items.Add(listItem);
      }
      this.Controls.Add(ListView1);
  }
  
' This method adds two columns to the ListView, setting the Text 
' and TextAlign, and Width properties of each ColumnHeader.  The 
' HeaderStyle property is set to NonClickable since the ColumnClick 
' event is not handled.  Finally the method adds ListViewItems and 
' SubItems to each column.
Private Sub InitializeListView()
    Me.ListView1 = New System.Windows.Forms.ListView
    Me.ListView1.BackColor = System.Drawing.SystemColors.Control
    Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top
    Me.ListView1.Location = New System.Drawing.Point(0, 0)
    Me.ListView1.Name = "ListView1"
    Me.ListView1.Size = New System.Drawing.Size(292, 130)
    Me.ListView1.TabIndex = 0
    Me.ListView1.View = System.Windows.Forms.View.Details
    Me.ListView1.MultiSelect = True
    Me.ListView1.HideSelection = False
    ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
    Dim columnHeader1 As New ColumnHeader
    With columnHeader1
        .Text = "Breakfast Item"
        .TextAlign = HorizontalAlignment.Left
        .Width = 146
    End With
    Dim columnHeader2 As New ColumnHeader
    With columnHeader2
        .Text = "Price Each"
        .TextAlign = HorizontalAlignment.Center
        .Width = 142
    End With

    Me.ListView1.Columns.Add(columnHeader1)
    Me.ListView1.Columns.Add(columnHeader2)
    Dim foodList() As String = New String() {"Juice", "Coffee", _
        "Cereal & Milk", "Fruit Plate", "Toast & Jelly", _
        "Bagel & Cream Cheese"}
    Dim foodPrice() As String = New String() {"1.09", "1.09", _
        "2.19", "2.49", "1.49", "1.49"}
    Dim count As Integer
    For count = 0 To foodList.Length - 1
        Dim listItem As New ListViewItem(foodList(count))
        listItem.SubItems.Add(foodPrice(count))
        ListView1.Items.Add(listItem)
    Next
    Me.Controls.Add(Me.ListView1)
End Sub

備註

會儲存顯示ListView在控制項中或指派給的ListViewGroup專案。 ListView.ListViewItemCollectionA ListView.ListViewItemCollection stores the items displayed in a ListView control or assigned to a ListViewGroup. ListView類別中定義了其他兩個集合,可讓您判斷在ListView控制項內選取了哪些專案。There are two other collections defined within the ListView class that enable you to determine what items are selected within a ListView control. ListView.SelectedIndexCollection ListView ListView ListView.ListViewItemCollection類別會提供屬性和方法來決定要在控制項中選取的專案,而類別可讓您判斷控制項中的哪些索引ListView.SelectedListViewItemCollection處於.The ListView.SelectedListViewItemCollection class provides properties and methods for determining what items are selected in a ListView control, while the ListView.SelectedIndexCollection class enables you to determine what indexes within the ListView.ListViewItemCollection of a ListView control are selected. 除了選擇集合類別之外,還有兩個類別,可讓您決定要檢查的CheckBoxes ListView專案(當控制項的屬性設定為true時)在此集合中。In addition to selection collection classes, there are also two classes that enable you to determine what items are checked (when the CheckBoxes property of the ListView control is set to true) within this collection.

有數種方式可將專案加入至集合。There are a number of ways to add items to the collection. Add方法會將單一專案新增至集合。The Add method adds a single item to the collection. 若要將多個專案加入至集合,請建立專案的陣列,並將它傳遞給AddRange方法。To add a number of items to the collection, you create an array of items and pass it to the AddRange method. 如果您想要在集合中的特定位置插入專案,可以使用Insert方法。If you want to insert an item at a specific location in the collection, you can use the Insert method. 若要移除專案,您可以使用Remove方法RemoveAt或方法(如果您知道專案位於集合中的位置)。To remove items, you can use either the Remove method or the RemoveAt method if you know where the item is located in the collection. 方法可讓您從集合中移除所有專案,而不是Remove使用方法一次移除單一專案。 ClearThe Clear method enables you to remove all items from the collection instead of using the Remove method to remove a single item at a time.

除了用於加入和移除專案的方法和屬性之外, ListView.ListViewItemCollection也提供方法來尋找集合中的專案。In addition to methods and properties for adding and removing items, the ListView.ListViewItemCollection also provides methods to find items in the collection. Contains方法可讓您判斷某個專案是否為集合的成員。The Contains method enables you to determine whether an item is a member of the collection. 一旦知道專案位於集合中,您就可以使用IndexOf方法來判斷專案位於集合中的位置。Once you know that the item is located in the collection, you can use the IndexOf method to determine where the item is located in the collection.

建構函式

ListView.ListViewItemCollection(ListView)

初始化 ListView.ListViewItemCollection 類別的新執行個體。Initializes a new instance of the ListView.ListViewItemCollection class.

屬性

Count

取得集合中的項目數目。Gets the number of items in the collection.

IsReadOnly

取得值,表示集合是否為唯讀。Gets a value indicating whether the collection is read-only.

Item[Int32]

取得或設定集合中位於指定索引處的項目。Gets or sets the item at the specified index within the collection.

Item[String]

擷取具有指定索引鍵的項目。Retrieves the item with the specified key.

方法

Add(ListViewItem)

將現有的 ListViewItem 加入至集合。Adds an existing ListViewItem to the collection.

Add(String)

建立具有指定之文字的項目,並將它加入集合中。Creates an item with the specified text and adds it to the collection.

Add(String, Int32)

使用指定的文字和影像建立項目,並將它加入至集合中。Creates an item with the specified text and image and adds it to the collection.

Add(String, String)

使用指定的文字和影像建立項目,並將它加入至集合中。Creates an item with the specified text and image and adds it to the collection.

Add(String, String, Int32)

使用指定的索引鍵、文字和影像建立項目,並將項目加入至集合中。Creates an item with the specified key, text, and image and adds an item to the collection.

Add(String, String, String)

使用指定的索引鍵、文字和影像建立項目,並將它加入至集合中。Creates an item with the specified key, text, and image, and adds it to the collection.

AddRange(ListView+ListViewItemCollection)

將項目的集合加入集合中。Adds a collection of items to the collection.

AddRange(ListViewItem[])

ListViewItem 物件陣列加入集合。Adds an array of ListViewItem objects to the collection.

Clear()

移除集合的所有項目。Removes all items from the collection.

Contains(ListViewItem)

判斷指定的項目是否位於集合中。Determines whether the specified item is located in the collection.

ContainsKey(String)

判斷集合是否包含具有指定索引鍵的項目。Determines whether the collection contains an item with the specified key.

CopyTo(Array, Int32)

將整個集合複製到現有陣列中的指定位置。Copies the entire collection into an existing array at a specified location within the array.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Find(String, Boolean)

搜尋名稱符合指定之索引鍵的項目,並選擇性地搜尋子項目。Searches for items whose name matches the specified key, optionally searching subitems.

GetEnumerator()

傳回列舉程式,其可用來在項目集合中重複。Returns an enumerator to use to iterate through the item collection.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(ListViewItem)

傳回指定項目集合中的索引。Returns the index within the collection of the specified item.

IndexOfKey(String)

擷取具有指定索引鍵之項目的索引。Retrieves the index of the item with the specified key.

Insert(Int32, ListViewItem)

將現有的 ListViewItem 插入位於指定索引處的集合中。Inserts an existing ListViewItem into the collection at the specified index.

Insert(Int32, String)

建立新的項目,並且將它插入位於指定索引處的集合中。Creates a new item and inserts it into the collection at the specified index.

Insert(Int32, String, Int32)

使用指定的影像索引建立新的項目後,並將它插入位於指定索引處的集合。Creates a new item with the specified image index and inserts it into the collection at the specified index.

Insert(Int32, String, String)

使用指定的文字和影像建立新的項目,並將它插入至集合中的指定索引處。Creates a new item with the specified text and image and inserts it in the collection at the specified index.

Insert(Int32, String, String, Int32)

使用指定的索引鍵、文字和影像建立新的項目,並將它插入至集合中的指定索引處。Creates a new item with the specified key, text, and image, and inserts it in the collection at the specified index.

Insert(Int32, String, String, String)

使用指定的索引鍵、文字和影像建立新的項目,並將它加入至集合中的指定索引處。Creates a new item with the specified key, text, and image, and adds it to the collection at the specified index.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Remove(ListViewItem)

從集合中移除指定的項目。Removes the specified item from the collection.

RemoveAt(Int32)

移除這個集合中位於指定索引處的項目。Removes the item at the specified index within the collection.

RemoveByKey(String)

從集合中移除具有指定索引鍵的項目。Removes the item with the specified key from the collection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

取得值,表示是否同步化存取集合 (執行緒安全)。Gets a value indicating whether access to the collection is synchronized (thread safe).

ICollection.SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。Gets an object that can be used to synchronize access to the collection of controls.

IList.Add(Object)

將現有物件加入至集合中。Adds an existing object to the collection.

IList.Contains(Object)

判斷指定的項目是否在集合中。Determines whether the specified item is in the collection.

IList.IndexOf(Object)

傳回指定項目集合中的索引。Returns the index within the collection of the specified item.

IList.Insert(Int32, Object)

插入物件至集合中的指定索引處。Inserts an object into the collection at the specified index.

IList.IsFixedSize

取得值,這個值表示集合的大小是否是固定的。Gets a value indicating whether the collection has a fixed size.

IList.Item[Int32]

取得或設定集合內位於指定索引處的 ListViewItemGets or sets the ListViewItem at the specified index within the collection.

IList.Remove(Object)

從集合中移除指定的項目。Removes the specified item from the collection.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱