By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,265 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,265 IT Pros & Developers. It's quick & easy.

Anyone: ListView Selected Index change seems to fire twice?

P: n/a

Hi All,
Is it normal that the ListView fires the selectedinexchange event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentException,
resulting in nItem not being initialised. That's why I have the first
Catch Block.

Is this normal, or is something a little goofy?

-nick

Private Sub lvLoaners_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lvLoaners.SelectedIndexChanged
Try

Dim nItem As Integer = lvLoaners.SelectedIndices(0)

Dim lvItem As ListViewItem = lvLoaners.Items(nItem)

Me.txtEditLoanerName.Text = lvItem.SubItems(0).Text
Me.txtEditLoanerPhone.Text = lvItem.SubItems(1).Text
Me.txtEditLoanerEmail.Text = lvItem.SubItems(2).Text

Catch a As ArgumentException
Return

Catch ex As Exception
MessageBox.Show("Error: " & ex.Message & " in
lvLoaners_SelectedIndexChanged()", "Error")

End Try
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have you read the manual?

for the ListView.SelectedIndexChanged event it says:

<quote>
In a multiple selection ListView control, this event occurs whenever an item
is removed or added to the list of selected items.
</quote>
"n. Smith" <ni***@yahoo.dk> wrote in message
news:g9********************************@4ax.com...

Hi All,
Is it normal that the ListView fires the selectedinexchange event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentException,
resulting in nItem not being initialised. That's why I have the first
Catch Block.

Is this normal, or is something a little goofy?

-nick

Private Sub lvLoaners_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lvLoaners.SelectedIndexChanged
Try

Dim nItem As Integer = lvLoaners.SelectedIndices(0)

Dim lvItem As ListViewItem = lvLoaners.Items(nItem)

Me.txtEditLoanerName.Text = lvItem.SubItems(0).Text
Me.txtEditLoanerPhone.Text = lvItem.SubItems(1).Text
Me.txtEditLoanerEmail.Text = lvItem.SubItems(2).Text

Catch a As ArgumentException
Return

Catch ex As Exception
MessageBox.Show("Error: " & ex.Message & " in
lvLoaners_SelectedIndexChanged()", "Error")

End Try

Nov 21 '05 #2

P: n/a
"n. Smith" <ni***@yahoo.dk> schrieb:
Is it normal that the ListView fires the selectedinexchange event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentException,
resulting in nItem not being initialised. That's why I have the first
Catch Block.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a

Mr Wagner,

Thanks a lot. An excellent solution.

Cheers,
Nick
On Tue, 3 May 2005 11:06:58 +0200, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote:
"n. Smith" <ni***@yahoo.dk> schrieb:
Is it normal that the ListView fires the selectedinexchange event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentException,
resulting in nItem not being initialised. That's why I have the first
Catch Block.


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


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.