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.ListViewItemCollection中。The following code example demonstrates how to add items to a ListView.ListViewItemCollection. 请注意,的ListView.ListViewItemCollection ListView1 Items属性的类型为。Note that the Items property of ListView1 is of type ListView.ListViewItemCollection. 若要运行此示例,请将以下代码ListView粘贴到包含名为ListView1的对象和名TextBoxTextBox1的的窗体中。To 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

注解

存储显示在控件中或分配给的ListViewGroup项。 ListView 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 ListView.SelectedIndexCollection ListView.ListViewItemCollection类提供用于确定控件中选定的项的属性和方法,而类可用于确定ListView控件的中的哪些索引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()

返回一个表示当前对象的 string。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

获取可用于同步控件集合访问的对象。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.

适用于

另请参阅