Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoid list view selected index changed firing twice

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?

like image 768
Alex Avatar asked Oct 06 '22 12:10

Alex


2 Answers

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

like image 103
stuartd Avatar answered Oct 10 '22 03:10

stuartd


i just tried another solution which is potentially without any delay, it worked for me:

    If ListView1.Items(ListView1.FocusedItem.Index).Selected = False Then
        'This is the deselected value
        MsgBox("Deselected: " & ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text)
    Else
        'This is the new selected value
        MsgBox("Selected: " & ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text)
    End If
like image 35
Preston777 Avatar answered Oct 10 '22 01:10

Preston777