Hello,
I'd Like to do the Same thing as in this post I posted a while back. But in a Listview Control http://www.daniweb.com/software-development/vbnet/threads/369754

But there is no Selected rows...

There is an Item Active But that did not work. And I cant find this anywhere else.
Can someone help?

Thanks,
Joshua K

P.S. for Topic I meant to put Help with ListView

Dani AI

Generated

Short version: the exception comes from indexing an empty SelectedItems collection while the ListView is mid-change. is right — SelectedIndexChanged can fire for the deselect step (no items selected) and then again for the new selection. That makes SelectedItems(0) invalid unless you check first.

A couple of safer patterns that avoid the error and handle single vs. multiple selection cleanly:

Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
    If e.IsSelected Then
        TextBox1.Text = e.Item.Text
    End If
End Sub

or, if you prefer to stay on SelectedIndexChanged, guard against an empty collection and use SelectedIndices:

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    If ListView1.SelectedIndices.Count = 0 Then Exit Sub
    Dim idx As Integer = ListView1.SelectedIndices(0)
    TextBox1.Text = ListView1.Items(idx).Text
End Sub

Other notes: if you only want action after the selection is stable, using BeginInvoke to defer the work or using ItemActivate (for double-click/Enter) can help. If MultiSelect is True, iterate SelectedItems instead of assuming a single item. Quick checklist: always test collection counts before indexing, consider FocusedItem for the currently focused row, and set MultiSelect/FullRowSelect according to the UI behavior you expect. These changes will stop the "index 0" error and make selection handling robust.

I did get it figured out.

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        TextBox1.Text = ListView1.SelectedItems(0).Text
    End Sub

But I keep getting an Error that I cant figure out.

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

It Works when I select the First thing. Like First Selection no matter what row. But when I go to select another row. It Gives me that Error.

Any Ideas?? Thanks

Member Avatar for Member #857553

Selected index changed will raise the event twice when changing. The first one will be a selected index of -1 when its deselecting the current item and the next will be the newly selected index. Its kind of stupid if you ask me. That had troubled me a couple of times.

Add

if selecteditems.count = 0 then exit sub at the top of the sub.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.