次の方法で共有


ListView.CheckedItems プロパティ

コントロールの現在チェックされている項目を取得します。

Public ReadOnly Property CheckedItems As _
   ListView.CheckedListViewItemCollection
[C#]
public ListView.CheckedListViewItemCollection CheckedItems {get;}
[C++]
public: __property ListView.CheckedListViewItemCollection*
   get_CheckedItems();
[JScript]
public function get CheckedItems() :
   ListView.CheckedListViewItemCollection;

プロパティ値

現在チェックされている項目を含んでいる ListView.CheckedListViewItemCollection 。項目が現在チェックされていない場合は、空の ListView.CheckedListViewItemCollection が返されます。

解説

このプロパティは、 ListView コントロールの CheckBoxes プロパティが true に設定されている場合にだけ有効です。 CheckedItems プロパティは、コントロールでチェックされているすべての項目を含むコレクションを返します。コレクション内の項目を操作する方法については、 ListView.CheckedListViewItemCollection のトピックを参照してください。

チェックされている項目ではなく、 ListView コントロールでチェックされている項目の ListView.ListViewItemCollection 内のインデックス位置のコレクションを取得する場合は、 CheckedIndices プロパティを使用します。

使用例

チェックされている ListView が System.Windows.Forms.ListView.ItemChecked イベントを処理するようすを次のコード例に示します。この例では、 CheckedItems プロパティを使用して、項目のチェック ボックスをオンまたはオフにするたびに新しい価格を合計しています。この例を実行するには、ListView1 という名前の ListView と Textbox1 という名前の TextBox が配置されているフォームに、次のコードを貼り付けます。そして、フォームのコンストラクタまたは Load メソッドから InititalizeListView メソッドを呼び出します。必ずすべてのイベントをイベント処理メソッドに関連付けるようにしてください。

[SampleID='System.Windows.Forms.ListView2' SnippetID='1,3']
--------- Languages displayed= cs, vb ---------
--------- cs ---------
--------- Snippet 3 ---------
    // Handles the ItemChecked event.  The method loops through all the 
    // checked items and tallies a new price each time an item is 
    // checked or unchecked. It outputs the price to TextBox1.
    private void ListView1_ItemCheck2(object sender, 
        System.Windows.Forms.ItemCheckEventArgs e)
    {
        double price = 0.0;
        ListView.CheckedListViewItemCollection checkedItems = 
            ListView1.CheckedItems;
        
        foreach ( ListViewItem item in checkedItems )
        {
            price += Double.Parse(item.SubItems[1].Text);
        }
        if (e.CurrentValue==CheckState.Unchecked)
        {
            price += Double.Parse(
                this.ListView1.Items[e.Index].SubItems[1].Text);
        }
        else if((e.CurrentValue==CheckState.Checked))
        {
            price -= Double.Parse(
                this.ListView1.Items[e.Index].SubItems[1].Text);
        }
        // Output the price to TextBox1.
        TextBox1.Text = price.ToString();
    }
--------- vb ---------
--------- Snippet 3 ---------
    ' Handles the ItemChecked event.  The method loops through all the 
    ' checked items and tallies a new price each time an item is 
    ' checked or unchecked. It outputs the price to TextBox1.
    Private Sub ListView1_ItemCheck2(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
        Handles ListView1.ItemCheck
        Dim item As ListViewItem
        Dim price As Double = 0.0
        Dim checkedItems As ListView.CheckedListViewItemCollection = _
            ListView1.CheckedItems
        For Each item In checkedItems
            price += Double.Parse(item.SubItems(1).Text)
        Next
        If (e.CurrentValue = CheckState.Unchecked) Then
            price += Double.Parse(Me.ListView1.Items(e.Index).SubItems(1).Text)
        ElseIf (e.CurrentValue = CheckState.Checked) Then
            price -= Double.Parse(Me.ListView1.Items(e.Index).SubItems(1).Text)
        End If
        ' Output the price to TextBox1.
        TextBox1.Text = CType(price, String)
      End Sub

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ListView クラス | ListView メンバ | System.Windows.Forms 名前空間 | ListView.CheckedListViewItemCollection | CheckedIndices