• 0

[VB.NET] Get index of selected item in listview


Question

Okay, basically, I want to get the index of the currently selected item (MultiSelect is set to False) in a ListView every time the selection changes. I'm using 'ListView.SelectedIndices.Item(0)' as the index of the currently selected item. It works fine when I use it on the Click event, but I want to get it every time the selection changes, whether by mouse or keyboard.

When I try to do it on the SelectedIndexChanged event, it works the first time, but the next time I select something, it gives me the error: ArgumentOutOfRangeException -- Value of '0' is not a valid for 'index' (referring to ListView.SelectedIndices.Item(0)). Can anybody please tell me what I'm doing wrong?

Thanks.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

That's because the event fires twice. Once for the item being selected, and once for the item that is no longer selected. Just check the SelectedItems.Count for > 0 before perforrming any operations on the first item in the collection.

Link to comment
Share on other sites

  • 0

Hi!

I know this is over 6 years old thread, but I really want to say thanks for this solution to the problem. I had identical problem and had been staring at the screen for quite a while. I didn't even think of event fireing twice. Wow. That's really wierd. :p

Cheers!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.