共用方式為


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
屬性
實作

範例

下列程式碼範例示範如何使用 SelectedIndicesSelectedIndexChangedHeaderStyle 成員和 ListView.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

備註

會將 ListView.SelectedIndexCollection 索引儲存至 控制項中 ListView 選取的專案。 中儲存 ListView.SelectedIndexCollection 的索引是 中的 ListView.ListViewItemCollection 索引位置。 會 ListView.ListViewItemCollection 儲存控制項中顯示的 ListView 所有專案。

下表顯示 如何 ListView.ListViewItemCollection 儲存 專案的範例, ListView 並在範例 ListView 中顯示其選取狀態。

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

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

索引 ListViewItemCollection 中選取專案的索引
0 1
1 3
2 4

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

建構函式

ListView.SelectedIndexCollection(ListView)

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

屬性

Count

取得集合中的項目數目。

IsReadOnly

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

Item[Int32]

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

方法

Add(Int32)

加入位於 Items 陣列中指定索引處的項目至集合。

Clear()

清除集合中的項目。

Contains(Int32)

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

CopyTo(Array, Int32)

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

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

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

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Int32)

ListView.SelectedIndexCollection 控制項的 ListView.ListViewItemCollection 傳回在所指定索引之 ListView 中的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Int32)

Items 移除位於 ListView.SelectedIndexCollection 集合中所指定索引處的項目。

ToString()

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

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

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

ICollection.SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。

IList.Add(Object)

將項目新增至集合。

IList.Clear()

移除集合的所有項目。

IList.Contains(Object)

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

IList.IndexOf(Object)

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

傳回 ListView.SelectedIndexCollection 中的索引。 ListView.SelectedIndexCollection 包含 ListView.ListViewItemCollection 控制項的 ListView 中已選取項目之索引。

IList.Insert(Int32, Object)

在指定的索引位置,插入項目至集合之中。

IList.IsFixedSize

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

IList.Item[Int32]

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

IList.Remove(Object)

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

IList.RemoveAt(Int32)

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

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於