Export (0) Print
Expand All

ListView.ItemCheck Event

Occurs when the check state of an item changes.

[Visual Basic]
Public Event ItemCheck As ItemCheckEventHandler
[C#]
public event ItemCheckEventHandler ItemCheck;
[C++]
public: __event ItemCheckEventHandler* ItemCheck;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type ItemCheckEventArgs containing data related to this event. The following ItemCheckEventArgs properties provide information specific to this event.

Property Description
CurrentValue Gets a value indicating the current state of the item's check box.
Index Gets the zero-based index of the item to change.
NewValue Gets or sets a value indicating whether to set the check box for the item to be checked, unchecked, or indeterminate.

Remarks

The ItemCheck event occurs when the check state of an item changes. The CheckBoxes property must be set to true in order for check boxes to be displayed next to each item in the ListView control. You can create an event handler for the ItemCheck event to perform tasks such as changing the state of an item's icon or another application-specific task each time an item is checked in the ListView control.

Note   If the Visible property of the ListView control is set to false at the time the control is created, the ItemCheck event is not raised. Once the control is visible, the event is raised regardless of the state of the Visible property.

For more information about handling events, see Consuming Events.

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 | OnItemCheck | ItemCheckEventHandler

Show:
© 2014 Microsoft