ListView.SelectedIndexChanged behavior

M

Mark Smith

I have a single-selection ListView where I always want to keep an item
selected by default. If the user clicks in the empty area below the
items, I want to select the first item.

It appears that when the user selects a new item, two
SelectedIndexChanged events are fired: one with no SelectedItems, and
then one with the new SelectedItem. Because of that, I can't tell if
an event with no selection is because the user clicked outside the
list items (and I should select the first one) or because they
selected another item (and I shouldn't).

Does anyone know a workaround for this?
 
J

Jared

From MouseClick event

MyListViewHitTestInfo = ListView1.HitTest(New Point(e.X, e.Y)

if MyListViewHitTestInfo.item is nothing then
me.ListView1.items(0).selected=true
me.ListView1.Select
End If



Should work well with you SIC event as well.
 
M

Mark Smith

From MouseClick event

MyListViewHitTestInfo = ListView1.HitTest(New Point(e.X, e.Y)

if MyListViewHitTestInfo.item is nothing then
me.ListView1.items(0).selected=true
me.ListView1.Select
End If

Hi Jared, thanks for the suggestion. Unfortunately, the ListView
doesn't fire a MouseClick event in the area below the items, so it
won't work...
 
J

Jared

MouseUp will do ...



Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp

Dim HTI0 As ListViewHitTestInfo

HTI0 = Me.ListView1.HitTest(New Point(e.X, e.Y))

If HTI0.Item Is Nothing Then

Trace.WriteLine(True)

Else

Trace.WriteLine(False)

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top