共用方式為


ListView.SelectedListViewItemCollection 類別

定義

代表清單檢視控制項中所選項目的集合。

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

範例

下列程式碼範例示範如何使用 SelectedItemsSelectedIndexChanged 事件和成員和 HeaderStyleListView.SelectedListViewItemCollection 類別。 若要執行此範例,請將下列程式碼貼到表單中,其中包含 ListView 名為 ListView1 的物件和 TextBox 名為 TextBox1 的 。 InitializeListView從表單的建構函式或 Load 事件處理常式呼叫 方法。 此範例要求事件處理常式與事件正確相關聯 SelectedIndexChanged

// 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.SelectedListViewItemCollection 選取的專案儲存在 控制項中 ListView 。 中 ListView.SelectedListViewItemCollection 儲存的專案是 中包含的 ListView.ListViewItemCollection 專案。 會 ListView.ListViewItemCollection 儲存 顯示在 中的所有 ListView 專案。

下表顯示 範例, ListView.ListViewItemCollection 說明 如何將 的專案 ListView 及其選取狀態儲存在範例 ListView 控制項中。

索引 Item ListView 中的選取狀態
0 Item1 未選取
1 Item2 已選取
2 Item3 未選取
3 Item4 已選取
4 Item5 已選取

下表根據 ListView.ListViewItemCollection 上表的範例,下表示范 的顯示方式 ListView.SelectedListViewItemCollection

索引 ListViewItemCollection 中選取的專案
0 Item2
1 Item4
2 Item5

您可以使用這個類別的屬性和方法,搭配集合執行各種工作。 方法 Contains 可讓您判斷 類別中的 ListView.ListViewItemCollection 專案是否為 的成員 ListView.SelectedListViewItemCollection 。 一旦您知道專案位於集合中,您就可以使用 IndexOf 方法來判斷 專案在 中 ListView.SelectedListViewItemCollection 的位置。

建構函式

ListView.SelectedListViewItemCollection(ListView)

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

屬性

Count

取得集合中的項目數目。

IsReadOnly

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

Item[Int32]

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

Item[String]

從集合中取得具有指定索引鍵的項目。

方法

Clear()

移除集合的所有項目。

Contains(ListViewItem)

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

ContainsKey(String)

判斷具有指定之按鍵的項目是否包含於集合中。

CopyTo(Array, Int32)

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

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

傳回列舉值,其可用來在選取項目集合中重複。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(ListViewItem)

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

IndexOfKey(String)

傳回第一個出現具有指定索引鍵之項目的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
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]

取得或設定集合中的物件。

IList.Remove(Object)

移除集合中第一個出現的指定項目。

IList.RemoveAt(Int32)

從指定索引的集合移除項目。

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於