JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Handling List View Item Selections

How can you determine which item was selected by the user in a list view? You can handle the SelectedIndexChanged event, and in this event's handler, you can check the list view's SelectedIndices to determine which items are currently selected. The SelectedIndices property holds the indices of those items that are selected if the list view's MultiSelect property is True, or of the single selected item, if one is selected, if MultiSelect is False. The MultiSelect property is False in the ListViews example on the CD-ROM, so here's how I display the currently selected item in the text box in that example:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    If ListView1.SelectedIndices.Count > 0 Then
        TextBox1.Text = "You clicked item " & _
            (ListView1.SelectedIndices(0) + 1)
    End If
End Sub

You can see the results in Figure 10.16, where I've clicked item 3 in the ListViews example, and the program is reporting that fact.

Click To expand
Figure 10.16: Handling list view item selections.

Besides SelectedIndices, you can also use a list view's SelectedItems property to get a collection of the selected items themselves.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor