By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Error with ListBox and ArrayList when Deleting Last Entry

P: n/a
My code works perfectly 100% when adding items to my ArrayList and updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and then crash after the return
from this code.

Now looking around the news groups it appears to be a bug in the .net implementation.

The only way I have found to get around it is to select the previous item before removing the last
item. I do this by setting the SelectedIndex to what it is -1. Now it works perfectly until there is
nothing left in the list to remove and I will assume it will go boom again then.

Does anyone know of another work around for this bug?

Also is it fixed in VS 2005?

Cheers
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
John,

The most made mistake with the listbox and combobox is that people forget
that when the index of that is changed, than direct the selectedindex change
is fired.

Can that be in this case as well the situation?

Cor
Nov 21 '05 #2

P: n/a
"John Veldthuis" <jo***@no.spamo.paradise.net.nz> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control? How
do you update the listbox control? I suggest to post some code.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
On Fri, 21 Oct 2005 12:53:43 +0200, "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote:
"John Veldthuis" <jo***@no.spamo.paradise.net.nz> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control? How
do you update the listbox control? I suggest to post some code.


dim ch1recipes as clsRecipes

file in the array here by loading from an SQL database.
for the listbox to display I simply set the datasource

lbRecipes.DataSource = ch1recipes

The class for ch1recipes has an override on the ToString method to display a full name. Everything
working fine to here.

Next remove the item if the user selects it and clicks the delete button.

lbRecipe.DataSource = Nothing
ch1recipes.Remove(lbRecipe.SelectedIndex) ' This removes the data from the ArrayList

' Remove the data from the SQL Database here

lbRecipe.DataSource = ch1recipes ' Needed to remove and add so that display is updated properly

Now everything works fine unless the item selected in the listbox is the last one. All the code
works fine. The item is removed properly from the arraylist and removed from the SQL database.
If it is the last one then as soon as the SelectedIndex method exits the subroutine it crashes. It
does not crash in user code so cannot be de-bugged.

Searching through the Google News I found several messages about exactly the same thing around 2002
and it was identified as a bug and reproducable 100% of the time. There was one suggetion as a work
around and that was to select the previous item in the listbox via code before removing the last
item in the arraylist. I tried this and it does cure the problem but is only a messy work around.

Nov 21 '05 #4

P: n/a
"John Veldthuis" <jo***@no.spamo.paradise.net.nz> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not
the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control?
How
do you update the listbox control? I suggest to post some code.


dim ch1recipes as clsRecipes

file in the array here by loading from an SQL database.
for the listbox to display I simply set the datasource

lbRecipes.DataSource = ch1recipes

The class for ch1recipes has an override on the ToString method to display
a full name. Everything
working fine to here.

Next remove the item if the user selects it and clicks the delete button.

lbRecipe.DataSource = Nothing
ch1recipes.Remove(lbRecipe.SelectedIndex) ' This removes the data from the
ArrayList

' Remove the data from the SQL Database here

lbRecipe.DataSource = ch1recipes ' Needed to remove and add so that
display is updated properly


You do not need to disconnect the arraylist from the listbox. Use the code
below instead to update the listbox control:

\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Load
m_Data.Add(New Foo("Bla", 12))
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.DataSource = m_Data
End Sub

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
m_Data.Add(New Foo("Goo", 13))
DirectCast(m_Data(0), Foo).Name = "Baz"

' Force update.
DirectCast( _
Me.ListBox1.BindingContext(Me.ListBox1.DataSource) , _
CurrencyManager _
).Refresh()
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
On Sat, 22 Oct 2005 13:08:14 +0200, "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote:
lbRecipe.DataSource = ch1recipes ' Needed to remove and add so that
display is updated properly
You do not need to disconnect the arraylist from the listbox. Use the code
below instead to update the listbox control:

' Force update.
DirectCast( _
Me.ListBox1.BindingContext(Me.ListBox1.DataSource) , _
CurrencyManager _
).Refresh()
End Sub
///


Thanks. I looked for something like this but could not see it. BindingContext does not show up under
intellisense either but it works.
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.