ListView OnSelectionChanged event firing multiple times

  • Thread starter Thread starter J.L.
  • Start date Start date
J

J.L.

Hi,

I have a ListView on my form whose selection is used to populate
several text fields. When selecting a ListView item, the data for the
previous selection is checked for errors. If errors are found, a
message box is displayed and the user is returned to the previously
selected index.

I am using the ListView as a single selection control so in the event
handler, I am only checking for errors when the selected items count is
equal to 1. I expect to have this event fired twice (once for the
user's selection and once when I manually select the previous item) but
I am seeing it fired four times.

For example, assume my ListView looks as follows:

Item A
Item B
Item C

Assume that Item B was selected and changes were made that contain
errors. When you select Item C, the errors are detected, but the
message box detecting the errors is displayed twice. If I have a
message box that displays the selected item when the event is handled,
I will see the message boxes in the following order:

Item C selected
Errors were detected
Item B selected
Item C selected
Errors were detected
Item B selected

In the end, I am returned to the previous selection and the behavior
that I need but I get the error message twice.

Does anyone know why that event is firing twice?

Thanks!!
 
As long as you dont incorperate multi-select..something like this should
do..

if me.lvListview1.SelectedItems.count = 0 then exit sub

You mean SelectedIndexChange event right.
 
Yes, I mean in the SelectedIndexChange event. Sorry if I wasn't clear.

I am already checking to see what the SelectedItems count is. The
message boxes that I described above are all occuring when
SelectedItems.count == 1. The first selection of Item C is when the
users clicks on the item. The first selection of Item B is when I
maunally select that item. What I don't understand is how the second
selection of Item C is occuring as the user is not clicking on Item C a
second time, nor am I manually selecting Item C.
 
Back
Top