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