ListView.ListViewItemCollection 類別

定義

代表 ListView 控制項中項目的集合,或指派給 ListViewGroup 之項目的集合。

public: ref class ListView::ListViewItemCollection : System::Collections::IList
public 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
[<System.ComponentModel.ListBindable(false)>]
type ListView.ListViewItemCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListView.ListViewItemCollection
Implements IList
繼承
ListView.ListViewItemCollection
屬性
實作

範例

下列程式碼範例示範如何將專案新增至 ListView.ListViewItemCollection 。 請注意, ItemsListView1 屬性的類型為 ListView.ListViewItemCollection 。 若要執行此範例,請將下列程式碼貼到表單中,其中包含 ListView 名為 ListView1 的物件和 TextBox 名為 TextBox1 的 。 InitializeListView從表單的建構函式或 Load 事件處理常式呼叫 方法。

// 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.ListViewItemCollection會儲存控制項中顯示的 ListView 專案,或指派給 ListViewGroup 。 類別中 ListView 定義了兩個其他集合,可讓您判斷控制項內 ListView 選取的專案。 類別 ListView.SelectedListViewItemCollection 提供屬性和方法來判斷控制項中 ListView 選取的專案,而 類別 ListView.SelectedIndexCollection 可讓您判斷控制項內 ListView.ListViewItemCollection 選取的 ListView 索引。 除了選取範圍集合類別之外,還有兩個類別可讓您決定當控制項的 ListView 屬性設定為此 true 集合內的) 時 CheckBoxes ,會 (檢查哪些專案。

有數種方式可將專案新增至集合。 方法 Add 會將單一專案加入至集合。 若要將一些專案新增至集合,您可以建立專案陣列,並將它傳遞至 AddRange 方法。 如果您想要在集合中的特定位置插入專案,您可以使用 Insert 方法。 若要移除專案,如果您知道專案位於集合中的位置,可以使用 Remove 方法或 RemoveAt 方法。 方法 Clear 可讓您從集合中移除所有專案,而不是使用 Remove 方法一次移除單一專案。

除了用於新增和移除專案的方法和屬性之外, ListView.ListViewItemCollection 也提供方法來尋找集合中的專案。 方法 Contains 可讓您判斷專案是否為集合的成員。 一旦您知道專案位於集合中,您就可以使用 IndexOf 方法來判斷專案在集合中的所在位置。

建構函式

ListView.ListViewItemCollection(ListView)

初始化 ListView.ListViewItemCollection 類別的新執行個體。

屬性

Count

取得集合中的項目數目。

IsReadOnly

取得值,表示集合是否為唯讀。

Item[Int32]

取得或設定集合中位於指定索引處的項目。

Item[String]

擷取具有指定索引鍵的項目。

方法

Add(ListViewItem)

將現有的 ListViewItem 加入至集合。

Add(String)

建立具有指定之文字的項目,並將它加入集合中。

Add(String, Int32)

使用指定的文字和影像建立項目,並將它加入至集合中。

Add(String, String)

使用指定的文字和影像建立項目,並將它加入至集合中。

Add(String, String, Int32)

使用指定的索引鍵、文字和影像建立項目,並將項目加入至集合中。

Add(String, String, String)

使用指定的索引鍵、文字和影像建立項目,並將它加入至集合中。

AddRange(ListView+ListViewItemCollection)

將項目的集合加入集合中。

AddRange(ListViewItem[])

ListViewItem 物件陣列加入集合。

Clear()

移除集合的所有項目。

Contains(ListViewItem)

判斷指定的項目是否位於集合中。

ContainsKey(String)

判斷集合是否包含具有指定索引鍵的項目。

CopyTo(Array, Int32)

將整個集合複製到現有陣列中的指定位置。

Equals(Object)

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

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

搜尋名稱符合指定之索引鍵的項目,並選擇性地搜尋子項目。

GetEnumerator()

傳回列舉程式,其可用來在項目集合中重複。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(ListViewItem)

傳回指定項目集合中的索引。

IndexOfKey(String)

擷取具有指定索引鍵之項目的索引。

Insert(Int32, ListViewItem)

將現有的 ListViewItem 插入位於指定索引處的集合中。

Insert(Int32, String)

建立新的項目,並且將它插入位於指定索引處的集合中。

Insert(Int32, String, Int32)

使用指定的影像索引建立新的項目後,並將它插入位於指定索引處的集合。

Insert(Int32, String, String)

使用指定的文字和影像建立新的項目,並將它插入至集合中的指定索引處。

Insert(Int32, String, String, Int32)

使用指定的索引鍵、文字和影像建立新的項目,並將它插入至集合中的指定索引處。

Insert(Int32, String, String, String)

使用指定的索引鍵、文字和影像建立新的項目,並將它加入至集合中的指定索引處。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(ListViewItem)

從集合中移除指定的項目。

RemoveAt(Int32)

移除這個集合中位於指定索引處的項目。

RemoveByKey(String)

從集合中移除具有指定索引鍵的項目。

ToString()

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

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

取得值,表示是否同步化存取集合 (執行緒安全)。

ICollection.SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。

IList.Add(Object)

將現有物件加入至集合中。

IList.Contains(Object)

判斷指定的項目是否在集合中。

IList.IndexOf(Object)

傳回指定項目集合中的索引。

IList.Insert(Int32, Object)

插入物件至集合中的指定索引處。

IList.IsFixedSize

取得值,這個值表示集合的大小是否是固定的。

IList.Item[Int32]

取得或設定集合內位於指定索引處的 ListViewItem

IList.Remove(Object)

從集合中移除指定的項目。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱