[RESOLVED] Problem with Listview SelectedIndexChanged event handler
I set up an event handler to catch when the user clicks on an item in a listview. The first time an item is clicked on, the SelectedIndices contains the index of the item selected and SelectedIndices.Count has a value of 1. After the first time when the event handler fires the count always has a value of zero and the SelectedIndices is always zero.
Thanks in advance,
Hal
up·grade (up'gräd'),
to take out old bugs
and put in new ones.
Re: Problem with Listview SelectedIndexChanged event handler
Thank you for replying. The style is single select. I don't see a SelectedIndex property. Just SelectedIndicies. This is what MSDN says about the SelectedIndicies property:
When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView.
up·grade (up'gräd'),
to take out old bugs
and put in new ones.
Re: Problem with Listview SelectedIndexChanged event handler
Strange... As far as I can see, there's no reason for this to happen. There must be some code interaction you're not aware of, that is not shown here. Are there other list views on your window? That could accidentally be set to use this event handler? (But still...) Or, maybe you accidentally unhooked lstvwAllProcesses from lstvwAllProcesses_SelectedIndexChanged and the event is actually fired by something else? (Seems unlikely, but that's just of the top of my head - you didn't provide an awful lot of detail.)
Make a quick, small test app with just a list view and see if it works properly. If it does, than it's something in your code: examine anything you think that could be relevant, and go step-by-step in the debugger, replicating the scenario, check every var (selectedProcessRowNumber, lstvwAllProcesses, sender).
Last edited by TheGreatCthulhu; October 11th, 2011 at 11:32 PM.
Re: Problem with Listview SelectedIndexChanged event handler
I have attached a test program. The form has one listview and nothing else.
I get the same results. The first time I click on an item the selected index is returned. The second time I get the error.
Thanks for the help.
Hal
up·grade (up'gräd'),
to take out old bugs
and put in new ones.
Re: Problem with Listview SelectedIndexChanged event handler
Figured it out. As it turns out, the SelectedIndexChanged event is fired more times then you'd intuitively expect. When you switch from a previously selected item to the next one, it's done in two steps: in the first one, the selected state changes from something to nothing, SelectedIndexChanged fires, and the SelectedIndices returns an empty collection (as nothing in is currently selected); in the second step, the new item get's selected, the SelectedIndexChanged event is fired again, and this time there will be an entry on SelectedIndices[0].
Microsoft kinda-sorta mentions this in the documentation:
A ListView.SelectedIndexCollection that contains the indexes of the selected items. If no items are currently selected, an empty ListView.SelectedIndexCollection is returned.
Hm... they should really elaborate on that a bit more...
Anyway, it's an easy fix:
Code:
if (listView1.SelectedIndices.Count > 0)
selectedItem = listView1.SelectedIndices[0];
If you worked with a multiselect listview, you could use foreach instead, as it will do nothing if the collection is empty.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.