avoid list view selected index changed firing twice

Alex picture Alex · Sep 26, 2012 · Viewed 8.4k times · Source

Quite a simple question, when the selected index of a list view is changed, the event fires twice, once for deselection and a second time to select the next item.

I need to use the event when selecting or deselecting at different times however whan deselecting only to reselect a moment later it makes half my ui flash from enabled being on to off and back on again, it also causes a fair bit of code to run so I just need a way of avoiding the deselection firing if it was another item that was clicked and not blank space (for deselection)

Dave R said to use a 100ms timer here : Am I missing something with my ListView selection event handling which sounds like it would work but seems quite untidy or generally a bad way of doing it.

My only other idea was to use the click event and then find the item at the location? but I'd rather not go to the hassle

thanks in advance!

-EDIT-

I've just thought that the click event would fire first so I could set a flag that skips selection index changed code if the click event happened on an item and then resets the flag after it's been used therefore skipping the deselection? I'll have a look now but again doesnt feel like a very efficient or easy way of doing something that sounds quite simple?

Answer

stuartd picture stuartd · Sep 26, 2012

Use the ItemSelectionChanged event instead - the ListViewItemSelectionChangedEventArgs can tell you which item caused it to fire, and whether it's selected or not.