語言

ListView.SelectedIndexCollection 類別

定義

代表包含控制項中所選項目 ListView 索引的集合。

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

範例

以下程式碼範例示範如何使用 SelectedIndices、 SelectedIndexChanged、 HeaderStyleListView.SelectedIndexCollection 以及 類別。 要執行此範例,請將以下程式碼貼入包含 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 SelectedIndices property to retrieve and tally the  
// price of the selected menu items.
void ListView1_SelectedIndexChanged_UsingIndices( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   ListView::SelectedIndexCollection^ indexes = this->ListView1->SelectedIndices;
   double price = 0.0;
   System::Collections::IEnumerator^ myEnum1 = indexes->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      int index = safe_cast<int>(myEnum1->Current);
      price += Double::Parse( this->ListView1->Items[ index ]->SubItems[ 1 ]->Text );
   }

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

    ListView.SelectedIndexCollection indexes = 
        this.ListView1.SelectedIndices;
    
    double price = 0.0;
    foreach ( int index in indexes )
    {
        price += Double.Parse(
            this.ListView1.Items[index].SubItems[1].Text);
    }

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

    Dim indexes As ListView.SelectedIndexCollection = _
        Me.ListView1.SelectedIndices
    Dim index As Integer
    Dim price As Double = 0.0
    For Each index In indexes
        price += Double.Parse(Me.ListView1.Items(index).SubItems(1).Text)
    Next

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

備註

A ListView.SelectedIndexCollection 將所選項目的索引儲存在 ListView 控制中。 儲存在 中的 ListView.SelectedIndexCollection 索引是 中索引位置 ListView.ListViewItemCollection。 它 ListView.ListViewItemCollection 會儲存所有在控制面板中 ListView 顯示的物品。

下表展示如何 ListView.ListViewItemCollection 儲存 項目 ListView 的範例,並以 為例 ListView顯示其選擇狀態。

索引 Item 列表視圖中的選擇狀態
0 Item1 未選取
1 Item2 已選擇
2 項目3 未選取
3 項目4 已選擇
4 第五項 已選擇

根據 ListView.ListViewItemCollection 前一表的例子,下表展示了 會 ListView.SelectedIndexCollection 如何出現。

索引 ListViewItemCollection 中所選項目的索引
0 1
1 3
2 4

你可以利用這個類別的屬性和方法來執行各種任務。 此 Contains 方法可判定 的 ListView.ListViewItemCollection 索引位置是否為儲存在 ListView.SelectedIndexCollection中的索引。 一旦你確定該項目在集合中,就可以使用該 IndexOf 方法來判斷索引在 ListView.SelectedIndexCollection中的位置。

建構函式

名稱 Description
ListView.SelectedIndexCollection(ListView)

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

屬性

名稱 Description
Count

取得集合中的項目數目。

IsReadOnly

會獲得一個值,表示該集合是否為唯讀。

Item[Int32]

在集合中指定的索引處取得索引值。

方法

名稱 Description
Add(Int32)

將陣列中指定索引 Items 的項目加入集合。

Clear()

清除收藏中的物品。

Contains(Int32)

判斷指定的索引是否位於集合中。

CopyTo(Array, Int32)

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

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

回傳一個枚舉器,可用於遍歷所選索引集合。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
IndexOf(Int32)

回傳控制項中指定索引ListView.SelectedIndexCollectionListView.ListViewItemCollection的ListView索引。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Remove(Int32)

從集合中移除集合中指定索引ItemsListView.SelectedIndexCollection的項目。

ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
ICollection.IsSynchronized

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

ICollection.SyncRoot

取得一個物件,可以用來同步存取控制項集合。

IList.Add(Object)

將專案加入至集合。

IList.Clear()

從集合中移除所有專案。

IList.Contains(Object)

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

IList.IndexOf(Object)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

回傳 中的索引 ListView.SelectedIndexCollection。 包含ListView.SelectedIndexCollection控制中選定項目ListView.ListViewItemCollectionListView的索引。

IList.Insert(Int32, Object)

在指定的索引處插入一個項目。

IList.IsFixedSize

取得值,指出 ListView.SelectedIndexCollection 是否有固定大小。

IList.Item[Int32]

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

IList.Remove(Object)

從集合中移除第一個出現的指定專案。

IList.RemoveAt(Int32)

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

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

將 IEnumerable 轉換成 IQueryable。

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於