ListView.SelectedListViewItemCollection 類別

定義

代表清單檢視控制項中選取項目的集合。Represents the collection of selected items in a list view control.

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

範例

下列程式碼範例將示範如何SelectedItems使用SelectedIndexChanged 、事件和HeaderStyle成員以及ListView.SelectedListViewItemCollection類別。The following code example demonstrates using the SelectedItems, SelectedIndexChanged event, and HeaderStyle members and the ListView.SelectedListViewItemCollection class. 若要執行此範例, 請將下列程式ListView代碼貼入包含名為ListView1之物件的表單TextBox1中, 以及名為的TextBoxTo run this example, paste the following code in 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 the Load event handler. 這個範例會要求事件處理常式與SelectedIndexChanged事件有正確的關聯This example requires that the event handler is correctly associated with the SelectedIndexChanged event

// 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
// Uses the SelectedItems property to retrieve and tally the price 
// of the selected menu items.
void ListView1_SelectedIndexChanged_UsingItems( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   ListView::SelectedListViewItemCollection^ breakfast = this->ListView1->SelectedItems;
   double price = 0.0;
   System::Collections::IEnumerator^ myEnum = breakfast->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current);
      price += Double::Parse( item->SubItems[ 1 ]->Text );
   }

   // Output the price to TextBox1.
   TextBox1->Text = price.ToString();
}
// Uses the SelectedItems property to retrieve and tally the price 
// of the selected menu items.
private void ListView1_SelectedIndexChanged_UsingItems(
    object sender, System.EventArgs e)
{

    ListView.SelectedListViewItemCollection breakfast = 
        this.ListView1.SelectedItems;
    
    double price = 0.0;
    foreach ( ListViewItem item in breakfast )
    {
        price += Double.Parse(item.SubItems[1].Text);
    }

    // Output the price to TextBox1.
    TextBox1.Text = price.ToString();
}
' Uses the SelectedItems property to retrieve and tally the price 
' of the selected menu items.
Private Sub ListView1_SelectedIndexChanged_UsingItems _
    (ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles ListView1.SelectedIndexChanged

    Dim breakfast As ListView.SelectedListViewItemCollection = _
        Me.ListView1.SelectedItems
    Dim item As ListViewItem
    Dim price As Double = 0.0
    For Each item In breakfast
        price += Double.Parse(item.SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)
End Sub

備註

會將選取的專案ListView 儲存在控制項中。ListView.SelectedListViewItemCollectionA ListView.SelectedListViewItemCollection stores the selected items in a ListView control. 儲存在中ListView.SelectedListViewItemCollection的專案是包含ListView.ListViewItemCollection在中的專案。The items stored in the ListView.SelectedListViewItemCollection are items contained in the ListView.ListViewItemCollection. 會儲存顯示在中的所有ListView專案。 ListView.ListViewItemCollectionThe ListView.ListViewItemCollection stores all items displayed in the ListView.

下表顯示如何ListView.ListViewItemCollection在範例ListView控制項中儲存的專案ListView , 以及其選取狀態的範例。The following table shows an example of how the ListView.ListViewItemCollection stores the items of the ListView as well as their selection states in an example ListView control.

索引Index 項目Item ListView 中的選取狀態Selection state in the ListView
00 Item1Item1 未選取Unselected
11 Item2Item2 已選取Selected
22 Item3Item3 未選取Unselected
33 Tuple.item4Item4 已選取Selected
44 Tuple.item5Item5 已選取Selected

根據上表中的ListView.SelectedListViewItemCollection 範例,下表將示範如何顯示。ListView.ListViewItemCollectionBased on the ListView.ListViewItemCollection example in the previous table, the following table demonstrates how the ListView.SelectedListViewItemCollection would appear.

索引Index ListViewItemCollection 中的已選取專案Selected item in the ListViewItemCollection
00 Item2Item2
11 Tuple.item4Item4
22 Tuple.item5Item5

您可以使用這個類別的屬性和方法, 利用集合來執行各種工作。You can use the properties and methods of this class to perform a variety of tasks with the collection. 方法可讓您判斷ListView.ListViewItemCollection類別中的專案是否ListView.SelectedListViewItemCollection為的成員。 ContainsThe Contains method enables you to determine whether an item from the ListView.ListViewItemCollection class is a member of the ListView.SelectedListViewItemCollection. 一旦知道專案是在集合中, 您就可以使用IndexOf方法來判斷專案ListView.SelectedListViewItemCollection在中的位置。Once you know that the item is in the collection, you can use the IndexOf method to determine the position of the item in the ListView.SelectedListViewItemCollection.

建構函式

ListView.SelectedListViewItemCollection(ListView)

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

屬性

Count

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

IsReadOnly

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

Item[Int32]

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

Item[String]

從集合中取得具有指定索引鍵的項目。Gets an item with the specified key from 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 an item with the specified key is contained in the collection.

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)
GetEnumerator()

傳回列舉值,其可用來在選取項目集合中重複。Returns an enumerator that can be used to iterate through the selected 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)

傳回第一個出現具有指定索引鍵之項目的索引。Returns the index of the first occurrence of the item with the specified key.

MemberwiseClone()

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

(繼承來源 Object)
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 item to the collection.

IList.Contains(Object)

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

IList.IndexOf(Object)

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

IList.Insert(Int32, Object)

在指定的索引位置,插入項目至集合之中。Inserts an item into the collection at a specified index.

IList.IsFixedSize

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

IList.Item[Int32]

取得或設定集合中的物件。Gets or sets an object from the collection.

IList.Remove(Object)

移除集合中第一個出現的指定項目。Removes the first occurrence of a specified item from the collection.

IList.RemoveAt(Int32)

從指定索引的集合移除項目。Removes an item from the collection at a specified index.

擴充方法

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.

適用於