Anyone: ListView Selected Index change seems to fire twice?

N

n. Smith

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
 
S

Stephany Young

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

Herfried K. Wagner [MVP]

n. Smith said:
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
///
 

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