About SelectedIndexChanged

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
 
G

Guest

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
 
C

Cor Ligthert

YXG,

Do we ask to much when we ask in what situation and what is the error?

Cor
 
J

Jerry Spence1

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
 
H

Herfried K. Wagner [MVP]

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'...
 
H

Herfried K. Wagner [MVP]

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
///
 
Y

yxq

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
///
 
J

Jerry Spence1

... 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
 
D

David

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.

:)
 
H

Herfried K. Wagner [MVP]

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)
 

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