[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
Re: Problem with Listview SelectedIndexChanged event handler
Is the list control style set to multi-select?
If not, use the SelectedIndex property to retrieve the index to the selected item.
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.
Re: Problem with Listview SelectedIndexChanged event handler
Ok. I was looking at the ListControl class instead of the ListView class.
Post your the code for the SelectedIndexChanged handler.
Re: Problem with Listview SelectedIndexChanged event handler
selectedProcessRowNumber is defined as:
private int selectedProcessRowNumber;
private void lstvwAllProcesses_SelectedIndexChanged(object sender, EventArgs e)
{
selectedProcessRowNumber = lstvwAllProcesses.SelectedIndices[0];
}
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).
Re: Problem with Listview SelectedIndexChanged event handler
Are you by chance hooking up more than one event handler?
E.g.
Code:
lstvwAllProcesses.SelectedIndexChanged += lstvwAllProcesses_SelectedIndexChanged;
Sometimes folks incorrectly hook up the handler more than once and it causes strange things to happen.
1 Attachment(s)
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
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:
Quote:
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.
Re: Problem with Listview SelectedIndexChanged event handler
It works fine now. Thanks for all the assistance. I really appreciate it.
Regards,
Hal