views:

484

answers:

1

The winform ListView doesn't seem to have an easy way to cancel the SelectedIndexChanged event. I don't see a SelectedIndexChanging event either.

The code is my attempt. However the hightlight is gone and I was wondering if I need to color the selection also or if there's a better way to cancel. _prevSelectedIndex is the index from the last selection. I want the highlight to go back to the previous selection.

 lvSearchResults.SelectedIndexChanged -= new EventHandler(lvSearchResults_SelectedIndexChanged);
lvSearchResults.SelectedIndices.Clear();
lvSearchResults.SelectedIndices.Add(_prevSelectedIndex);
lvSearchResults.Items[_prevSelectedIndex].Selected = true;
lvSearchResults.SelectedIndexChanged += new EventHandler(lvSearchResults_SelectedIndexChanged);
A: 

As long as you have multiselect turned off you shouldn't have to call SelectedIndices.Clear() Selectedndices.Add(..) as well as Items[_prevSelectedIndex].Selected = true; - the latter will accomplish the same result.

I believe you're also adding a continually growing list of event handlers. To remove an event handler you have to store an instance of the delegate then remove that instance; -= new EventHandler(...) isn't going to remove the handler.

Arnshea
Abdu
... infinite loop because of the 'Selected = true' statement.
Abdu