473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

About SelectedIndexChanged

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
Nov 21 '05 #1
10 1985
YXG,

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

Cor

"yxq" <ga***@163.net>
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

Nov 21 '05 #2
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.T ostring)
endif
End Sub

Nov 21 '05 #3
What is the error?
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"yxq" <ga***@163.net> wrote in message
news:ew**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #4

"Mike McIntyre" <mi****@dotnetshowandtell.com> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
What is the error?
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"yxq" <ga***@163.net> wrote in message
news:ew**************@TK2MSFTNGP14.phx.gbl...
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


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
Nov 21 '05 #5
"Jerry Spence1" <je**********@somewhere.com> schrieb:
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'...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #6
"yxq" <ga***@163.net> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7
yxq
Thank you very much!

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> дÈëÏûÏ¢ÐÂÎÅ:ur**************@tk2msftngp13.phx.gbl ...
"yxq" <ga***@163.net> schrieb:
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
///

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

Nov 21 '05 #8
... should read 'ListView1.SelectedItems.Count > 0'...

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

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
Nov 21 '05 #9
On 2004-12-11, Jerry Spence1 <je**********@somewhere.com> wrote:
... should read 'ListView1.SelectedItems.Count > 0'...

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

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.

:)

Nov 21 '05 #10
David,

"David" <df*****@woofix.local.dom> schrieb:
... 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.


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)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: LV | last post by:
Hello, I would like to manually set one of my list view items as selected. When this item is set, I would like for a method to execute. I have a delegate on the list view for SelectedIndexChanged....
0
by: LV | last post by:
Hello, When I manually set a list view item to be a selected item, the SelectedIndexChanged event is not firing. Am I missing something here? using System; using System.Windows.Forms; ...
1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
0
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
0
by: tafpin | last post by:
I have an application with a datagrid. In the IDE I have 2 template columns. The first has an image button and the second contains a link button. According to the results that I get back I must...
2
by: rdb | last post by:
VB.NET web program with a webform w/2 dropdownlistboxes, set to AutoPostBack TRUE, selection in either dropdown fires the SelectedIndexChanged events correctly UNTIL I navigate to second webform in...
7
by: sparkle | last post by:
Hi Everybody, I'm filling a combobox from a class, which works fine on it's own. But when I insert code to fill in other controls something in the combobox fill is causing the...
2
by: Dave A | last post by:
I am stuggling with databinding a drop down list, hooking into the SelectedIndexChanged and attempting to avoid using the viewstate. The drop down list is quite large so I would prefer to avoid...
3
by: martin1 | last post by:
Hi, All, I want user select first item (called All) in listbox, then all other items are selected by SetSelected method, but in loop (see code below) whenever going to SetSelected(), the...
4
by: lakepeir | last post by:
Hello, I have combobox with a selectedindexchanged method that seems to be called when starting the application, launching the form with the combobox and making a change in the drop down box of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.