Bug in Visual Studio 2005 Listview object. Help!

C

cr113

I've found an obvious bug in VS2005 (VB.NET), in the Listview object.
I desperately need a fix. To duplicate do the following:

1. Create a new Visual Basic Windows Application.

2. Add a Listview object and Textbox object to the Form.

3. Add a column to the Listview Column collection.

4. Set the Listview View property to Details.

5. Set the Listview FullRowSelect property to True.

6. Add the following code to the Form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
ListView1.Items.Add("aaa")
ListView1.Items.Add("bbb")
ListView1.Items.Add("ccc")
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
TextBox1.Text = ListView1.SelectedItems(0).Text
End Sub

Click on a few different rows in the Listview box and you'll get an
error saying the selected index is not set. It appears that the
selected index collection is being updated AFTER the
SelectedIndexChanged event. Is there a fix for this?

I've tried searching thru Microsoft to see if I'm missing the latest
Service Pack for Visual Studio 2005 but have had no luck.

Thanks!
 
J

Jack Jackson

I've found an obvious bug in VS2005 (VB.NET), in the Listview object.
I desperately need a fix. To duplicate do the following:

1. Create a new Visual Basic Windows Application.

2. Add a Listview object and Textbox object to the Form.

3. Add a column to the Listview Column collection.

4. Set the Listview View property to Details.

5. Set the Listview FullRowSelect property to True.

6. Add the following code to the Form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
ListView1.Items.Add("aaa")
ListView1.Items.Add("bbb")
ListView1.Items.Add("ccc")
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
TextBox1.Text = ListView1.SelectedItems(0).Text
End Sub

Click on a few different rows in the Listview box and you'll get an
error saying the selected index is not set. It appears that the
selected index collection is being updated AFTER the
SelectedIndexChanged event. Is there a fix for this?

I've tried searching thru Microsoft to see if I'm missing the latest
Service Pack for Visual Studio 2005 but have had no luck.

Thanks!

My guess is that sometimes SelectedIndexChanged is called when the
SelectedItems collection has no entries. It is perfectly valid for
there to be no rows selected in a ListView. Change your code to:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
TextBox1.Text = If(ListView1.SelectedItems.Count > 0,
ListView1.SelectedItems(0).Text, '')
End Sub

It also might be necessary to check Listview1.SelectedItems for
Nothing.

You should be able to easily figure out what is going on by using the
debugger and looking at ListView1.SelectedItems when it traps.
 
R

Registered User

I've found an obvious bug in VS2005 (VB.NET), in the Listview object.
I desperately need a fix. To duplicate do the following:

1. Create a new Visual Basic Windows Application.

2. Add a Listview object and Textbox object to the Form.

3. Add a column to the Listview Column collection.

4. Set the Listview View property to Details.

5. Set the Listview FullRowSelect property to True.

6. Add the following code to the Form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
ListView1.Items.Add("aaa")
ListView1.Items.Add("bbb")
ListView1.Items.Add("ccc")
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
TextBox1.Text = ListView1.SelectedItems(0).Text
End Sub

Click on a few different rows in the Listview box and you'll get an
error saying the selected index is not set. It appears that the
selected index collection is being updated AFTER the
SelectedIndexChanged event. Is there a fix for this?
This isn't a bug. Add some exception handling to the
SelectedIndexChanged method and examine what occurs. You will see this
event gets fired twice.

Let us say that the selected indexed is 0 and the user clicks on the
second item in the list. The first thing that happens is item 0 is
unselected. The selected index has changed because no item is selected
and the event fires. The selected items collection now contains zero
items which is the cause of the exception.

The event is fired again when the second item in the list gets added
to the selected items collection. The collection count goes from zero
to one.

regards
A.G.
 
C

cr113

This isn't a bug. Add some exception handling to the
SelectedIndexChanged method and examine what occurs. You will see this
event gets fired twice.

Let us say that the selected indexed is 0 and the user clicks on the
second item in the list. The first thing that happens is item 0 is
unselected. The selected index has changed because no item is selected
and the event fires. The selected items collection now contains zero
items which is the cause of the exception.

The event is fired again when the second item in the list gets added
to the selected items collection. The collection count goes from zero
to one.

Excellent! That makes sense.

In my defense, it didn't do that in VS 2003.

Thanks, you saved me!
 

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