CheckedItems Property

ListView.CheckedItems Property

Gets the currently checked items in the control.

[Visual Basic]
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;

Property Value

A ListView.CheckedListViewItemCollection that contains the currently checked items. If no items are currently checked, an empty ListView.CheckedListViewItemCollection is returned.

Remarks

This property is only useful when the CheckBoxes property of the ListView control is set to true. The CheckedItems property returns a collection containing all items that are checked in the control. For more information on how to manipulate the items in the collection, see ListView.CheckedListViewItemCollection.

If you want to obtain a collection of the index positions within the ListView.ListViewItemCollection of the items that are checked in the ListView control, instead of the items that are checked, use the CheckedIndices property.

Example

The following code example demonstrates a checked ListView that handles the System.Windows.Forms.ListView.ItemChecked event. The example uses the CheckedItems property to tally a new price each time an item is checked or unchecked. To run this example paste the following code in a form containing a ListView named ListView1 and a TextBox named Textbox1. Call the InititalizeListView method from the form's constructor or Load method. Ensure all events are connected to their event-handling methods.

[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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ListView Class | ListView Members | System.Windows.Forms Namespace | ListView.CheckedListViewItemCollection | CheckedIndices

Show:
© 2016 Microsoft