ListView.SelectedListViewItemCollection クラス
リスト ビュー コントロールにある、選択された項目のコレクションを表します。
この型のすべてのメンバの一覧については、ListView.SelectedListViewItemCollection メンバ を参照してください。
System.Object
System.Windows.Forms.ListView.SelectedListViewItemCollection
Public Class ListView.SelectedListViewItemCollection
Implements IList, ICollection, IEnumerable
[C#]
public class ListView.SelectedListViewItemCollection : IList,
ICollection, IEnumerable
[C++]
public __gc class ListView.SelectedListViewItemCollection : public
IList, ICollection, IEnumerable
[JScript]
public class ListView.SelectedListViewItemCollection implements
IList, ICollection, IEnumerable
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
ListView.SelectedListViewItemCollection は ListView コントロールの選択された項目を格納します。 ListView.SelectedListViewItemCollection に格納されている項目は、 ListView.ListViewItemCollection に含まれている項目です。 ListView.ListViewItemCollection は、 ListView に表示されるすべての項目を格納します。
ListView の項目が ListView.ListViewItemCollection にどのように格納されているか、およびそれらの項目の ListView コントロール内での選択の状態を次の表に示します。
インデックス | 項目 | 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 内の項目の位置を確認できます。
使用例
[Visual Basic, C#] SelectedItems イベント、 SelectedIndexChanged イベント、 HeaderStyle のメンバ、および System.Windows.Forms.SelectedListViewItemCollection クラスを使用するコード例を次に示します。この例を実行するには、ListView1 という名前の ListView オブジェクトおよび TextBox1 という名前の TextBox が配置されているフォームに、次のコードを貼り付けます。そして、フォームのコンストラクタまたは Load メソッドから InitializeListView メソッドを呼び出します。この例は、イベントを処理するメソッドが 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.
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.
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
[C#]
// 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);
}
. . .
// 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();
}
[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Windows.Forms
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)
参照
ListView.SelectedListViewItemCollection メンバ | System.Windows.Forms 名前空間