Groups | Blog | Home
all groups > vb.net > december 2004 >

vb.net : About SelectedIndexChanged


Rulin Hong
12/10/2004 7:25:41 AM
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
12/10/2004 7:29:13 AM
What is the error?


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

[quoted text, click to view]

Cor Ligthert
12/10/2004 4:26:54 PM
YXG,

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

Cor

"yxq" <gayxq@163.net>

[quoted text, click to view]

Jerry Spence1
12/10/2004 6:00:06 PM

[quoted text, click to view]

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

Herfried K. Wagner [MVP]
12/10/2004 8:51:42 PM
"Jerry Spence1" <jerry.spence@somewhere.com> schrieb:
[quoted text, click to view]

.... should read 'ListView1.SelectedItems.Count > 0'...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Herfried K. Wagner [MVP]
12/10/2004 8:55:26 PM
"yxq" <gayxq@163.net> schrieb:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

yxq
12/10/2004 10:18:46 PM
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

David
12/11/2004 6:09:34 AM
[quoted text, click to view]


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

:)
yxq
12/11/2004 7:58:55 AM
Thank you very much!

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> дÈëÏûÏ¢ÐÂÎÅ:urlDzIv3EHA.1392@tk2msftngp13.phx.gbl...
[quoted text, click to view]

Jerry Spence1
12/11/2004 1:58:13 PM

[quoted text, click to view]
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

Herfried K. Wagner [MVP]
12/11/2004 7:35:50 PM
David,

"David" <dfoster@woofix.local.dom> schrieb:
[quoted text, click to view]

:-)))

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

AddThis Social Bookmark Button