PC Review


Reply
Thread Tools Rate Thread

Bug in Visual Studio 2005 Listview object. Help!

 
 
cr113
Guest
Posts: n/a
 
      15th Nov 2009

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!
 
Reply With Quote
 
 
 
 
Jack Jackson
Guest
Posts: n/a
 
      15th Nov 2009
On Sun, 15 Nov 2009 10:52:00 -0800 (PST), cr113 <(E-Mail Removed)>
wrote:

>
>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.
 
Reply With Quote
 
Registered User
Guest
Posts: n/a
 
      15th Nov 2009
On Sun, 15 Nov 2009 10:52:00 -0800 (PST), cr113 <(E-Mail Removed)>
wrote:

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

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

 
Reply With Quote
 
cr113
Guest
Posts: n/a
 
      16th Nov 2009
On Nov 15, 1:46*pm, Registered User <n4...@ix.netcom.com> wrote:
> On Sun, 15 Nov 2009 10:52:00 -0800 (PST),cr113<cr...@hotmail.com>
> wrote:
>
>
>
>
>
> >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.


Excellent! That makes sense.

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

Thanks, you saved me!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to take an IE rendered screenshot of a website with visual studio .net 2002 or visual stuido .net 2003? I can't install visual studio .net 2005 on this computer. Daniel Microsoft Dot NET 4 17th May 2007 07:56 PM
How to take an IE rendered screenshot of a website with visual studio .net 2002 or visual stuido .net 2003? I can't install visual studio .net 2005 on this computer. Daniel Microsoft Dot NET Framework 1 14th May 2007 08:05 PM
How to take an IE rendered screenshot of a website with visual studio .net 2002 or visual stuido .net 2003? I can't install visual studio .net 2005 on this computer. Daniel Microsoft C# .NET 0 14th May 2007 07:45 PM
object browser with Visual Studio.Net 2005 Vivek N Microsoft ASP .NET 1 19th Jul 2006 07:21 PM
Visual Studio 2005 Beta 1 Refresh vs Visual Studio 2005 CTP vs Visual C++ 2005 Express Beta1 Peter Nimmo Microsoft VC .NET 1 16th Dec 2004 03:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:20 AM.