ListView.ListViewItemCollection ListView.ListViewItemCollection ListView.ListViewItemCollection ListView.ListViewItemCollection Class

定义

表示 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.ListViewItemCollectionListView.ListViewItemCollectionListView.ListViewItemCollectionListView.ListViewItemCollection
属性
实现

示例

下面的代码示例演示如何将项添加到ListView.ListViewItemCollectionThe following code example demonstrates how to add items to a ListView.ListViewItemCollection. 请注意,Items的属性ListView1属于类型ListView.ListViewItemCollectionNote that the Items property of ListView1 is of type ListView.ListViewItemCollection. 若要运行此示例,请将以下代码粘贴到包含一个窗体ListView名为对象ListView1和一个TextBox名为TextBox1To run this example, paste the following code into a form that contains a ListView object named ListView1 and a TextBox named TextBox1. 调用InitializeListView方法从窗体的构造函数或Load事件处理程序。Call 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.ListViewItemCollection存储中显示的项ListView控制或分配给ListViewGroupA 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.SelectedListViewItemCollection类提供的属性和方法来确定在选择了哪些项ListView控件,而ListView.SelectedIndexCollection类使您能够确定哪些索引内ListView.ListViewItemCollectionListView控制是在选择。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. Clear方法使您能够从而不是使用集合中移除所有项Remove方法一次都删除单个项。The 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(ListView) ListView.ListViewItemCollection(ListView) ListView.ListViewItemCollection(ListView)

初始化 ListView.ListViewItemCollection 类的新实例。Initializes a new instance of the ListView.ListViewItemCollection class.

属性

Count Count Count Count

获取集合中的项数。Gets the number of items in the collection.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

获取一个值,该值指示集合是否为只读。Gets a value indicating whether the collection is read-only.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取或设置集合中指定索引处的项。Gets or sets the item at the specified index within the collection.

Item[String] Item[String] Item[String] Item[String]

检索具有指定键的项。Retrieves the item with the specified key.

方法

Add(ListViewItem) Add(ListViewItem) Add(ListViewItem) Add(ListViewItem)

将现有的 ListViewItem 添加到集合中。Adds an existing ListViewItem to the collection.

Add(String) Add(String) Add(String) Add(String)

用指定的文本创建一个项并将该项添加到集合中。Creates an item with the specified text and adds it to the collection.

Add(String, Int32) Add(String, Int32) Add(String, Int32) Add(String, Int32)

用指定的文本和图像创建一个项并将该项添加到集合中。Creates an item with the specified text and image and adds it to the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

用指定的文本和图像创建一个项并将该项添加到集合中。Creates an item with the specified text and image and adds it to the collection.

Add(String, String, Int32) Add(String, String, Int32) Add(String, String, Int32) 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) Add(String, String, String) Add(String, String, String) Add(String, String, String)

用指定的键、文本和图像创建一个项并将该项添加到集合中。Creates an item with the specified key, text, and image, and adds it to the collection.

AddRange(ListView+ListViewItemCollection) AddRange(ListView+ListViewItemCollection) AddRange(ListView+ListViewItemCollection) AddRange(ListView+ListViewItemCollection)

向集合中添加项的集合。Adds a collection of items to the collection.

AddRange(ListViewItem[]) AddRange(ListViewItem[]) AddRange(ListViewItem[]) AddRange(ListViewItem[])

向集合中添加 ListViewItem 对象的数组。Adds an array of ListViewItem objects to the collection.

Clear() Clear() Clear() Clear()

从集合中移除所有项。Removes all items from the collection.

Contains(ListViewItem) Contains(ListViewItem) Contains(ListViewItem) Contains(ListViewItem)

确定指定项是否位于集合内。Determines whether the specified item is located in the collection.

ContainsKey(String) ContainsKey(String) ContainsKey(String) ContainsKey(String)

确定集合是否包含具有指定键的项。Determines whether the collection contains an item with the specified key.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

将整个集合复制到现有数组中,从该数组内的指定位置开始复制。Copies the entire collection into an existing array at a specified location within the array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Find(String, Boolean) Find(String, Boolean) Find(String, Boolean) Find(String, Boolean)

搜索名称与指定键匹配的项,也可以选择搜索子项。Searches for items whose name matches the specified key, optionally searching subitems.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回一个枚举数,将使用该枚举数循环访问项集合。Returns an enumerator to use to iterate through the item collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IndexOf(ListViewItem) IndexOf(ListViewItem) IndexOf(ListViewItem) IndexOf(ListViewItem)

返回指定的项在集合中的索引。Returns the index within the collection of the specified item.

IndexOfKey(String) IndexOfKey(String) IndexOfKey(String) IndexOfKey(String)

检索具有指定键的项的索引。Retrieves the index of the item with the specified key.

Insert(Int32, ListViewItem) Insert(Int32, ListViewItem) Insert(Int32, ListViewItem) Insert(Int32, ListViewItem)

将现有的 ListViewItem 插入到集合中的指定索引处。Inserts an existing ListViewItem into the collection at the specified index.

Insert(Int32, String) Insert(Int32, String) Insert(Int32, String) Insert(Int32, String)

创建一个新项,并将其插入到集合中的指定索引处。Creates a new item and inserts it into the collection at the specified index.

Insert(Int32, String, Int32) Insert(Int32, String, Int32) Insert(Int32, String, Int32) 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) Insert(Int32, String, String) Insert(Int32, String, String) 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) Insert(Int32, String, String, Int32) Insert(Int32, String, String, Int32) 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) Insert(Int32, String, String, String) Insert(Int32, String, String, String) 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() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(ListViewItem) Remove(ListViewItem) Remove(ListViewItem) Remove(ListViewItem)

从集合中移除指定的项。Removes the specified item from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

移除集合中指定索引处的项。Removes the item at the specified index within the collection.

RemoveByKey(String) RemoveByKey(String) RemoveByKey(String) RemoveByKey(String)

从集合中移除具有指定键的项。Removes the item with the specified key from the collection.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。Gets a value indicating whether access to the collection is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步控件集合访问的对象。Gets an object that can be used to synchronize access to the collection of controls.

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

将一个现有对象添加到集合中。Adds an existing object to the collection.

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

确定指定项是否在集合中。Determines whether the specified item is in the collection.

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

返回指定的项在集合中的索引。Returns the index within the collection of the specified item.

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

将对象插入到集合中的指定索引处。Inserts an object into the collection at the specified index.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

获取一个值,该值指示集合是否具有固定大小。Gets a value indicating whether the collection has a fixed size.

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

获取或设置集合中指定索引处的 ListViewItemGets or sets the ListViewItem at the specified index within the collection.

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

从集合中移除指定的项。Removes the specified item from the collection.

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

将转换IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅