About SelectedIndexChanged

  • Thread starter Thread starter yxq
  • Start date Start date
Y

yxq

Hello,
There are some items in ListView1, but error will occur in the code below,
why? Thank you.

****************************************************************
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ListView1.SelectedIndexChanged
MessageBox.Show(ListView1.SelectedItems(0).Index)
End Sub
 
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ListView1.SelectedIndexChanged
if ListView1.SelectedItems.count>0 then
MessageBox.Show(ListView1.SelectedItems(0).Index.Tostring)
endif
End Sub
 
Mike McIntyre said:
What is the error?


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

Yes I've had that. At the instance of selecting, the event is fired with no
items selected. I got round it by putting in a catch at the top

If listview1.SelectedItems = 0 then Exit Sub

I don't know why it does it.

- Jerry
 
Jerry Spence1 said:
Yes I've had that. At the instance of selecting, the event is fired with
no
items selected. I got round it by putting in a catch at the top

If listview1.SelectedItems = 0 then Exit Sub

.... should read 'ListView1.SelectedItems.Count > 0'...
 
yxq said:
There are some items in ListView1, but error will occur in the code below,
why? Thank you.

****************************************************************
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ListView1.SelectedIndexChanged
MessageBox.Show(ListView1.SelectedItems(0).Index)
End Sub

That's not a bug. It's a feature.

When the user initiates a selection change that effectively results in all
selected items being deselected and then one or more items being
(re)selected, the control will raise the event twice, first for deselecting
the items and then for selecting them again (although the user will only see
that as a single operation). You can avoid the exception by extending your
code like this:

\\\
Dim SourceControl As ListView = DirectCast(sender, ListView)
If SourceControl.SelectedItems.Length > 0 Then
...
End If
///
 
Thank you very much!

Herfried K. Wagner said:
That's not a bug. It's a feature.

When the user initiates a selection change that effectively results in all
selected items being deselected and then one or more items being
(re)selected, the control will raise the event twice, first for
deselecting the items and then for selecting them again (although the user
will only see that as a single operation). You can avoid the exception by
extending your code like this:

\\\
Dim SourceControl As ListView = DirectCast(sender, ListView)
If SourceControl.SelectedItems.Length > 0 Then
...
End If
///
 
... should read 'ListView1.SelectedItems.Count > 0'...
I think I was right the first time. If there are no selected items, then get
out. It happens when the event fires for the first time.

- Jerry
 
I think I was right the first time. If there are no selected items, then get
out. It happens when the event fires for the first time.


Of course, your original code snippet does have the minor disadvantage
of not compiling. That can be a detriment in certain applications.

:)
 
David,

David said:
Of course, your original code snippet does have the minor disadvantage
of not compiling. That can be a detriment in certain applications.

:)

:-)))

"It doesn’t matter how fast your code is
if it doesn’t work."
-- Joe Hacker in "Hardcore Visual Basic" (Bruce McKinney)
 
Back
Top