Sorry I don't have an easy solution, I would just like
to comment on your idea
Using the SelectedIndexChanged event isn't that simple.
Imagine that you have one item selected and that you
click on another item. This is what happens:
1. Windows sends a notification (LVN_ITEMCHANGED)
that the previously selected item has been deselected.
2. NET handles that notification and fires a
SelectedIndexChanged event. At this point you have no
selected items.
3. Windows sends a notification (LVN_ITEMCHANGED)
that the new item has been selected.
4. NET handles that notification and fires a
SelectedIndexChanged event. At this point you have 1
selected item.
Invoking a method asynchronously (using Control.BeginInvoke)
from inside the SelectedIndexChanged handler might work
That uses PostMessage to place a message into the message
queue and your method is called when that message is handled.
Since it's placed last in the queue it should be handled after
the last LVN_ITEMCHANGED notification and allow you
to reliably check if the selection count is 0
Something like this seems to be working:
Private m_item As ListViewItem = Nothing
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
ListView1.BeginInvoke(New MethodInvoker(AddressOf
Me.HandleSelectionChange))
End Sub
Private Sub HandleSelectionChange()
If ListView1.SelectedItems.Count = 0 Then
If Not m_item Is Nothing Then
RemoveHandler ListView1.SelectedIndexChanged, AddressOf
Me.ListView1_SelectedIndexChanged
m_item.Selected = True
AddHandler ListView1.SelectedIndexChanged, AddressOf
Me.ListView1_SelectedIndexChanged
End If
Else
m_item = ListView1.SelectedItems(0)
End If
End Sub
/claes